Você está na página 1de 9

In this tutorial you will learn about php, this tutorial is specially made for

people who has never even written in any programming language, or has just wanted
to learn php.

This tutorial will help you, it will show you the basics of php, and answer some
commonly asked questions.

Well, I thought before you started learning, it would be a good idea to tell you
what php can, and cannot do.

PHP, Short for Hypertext Pre-processor, is among the most powerful programming
languages to write websites with.
A script is firstly processed by the server, something like Apache, and then sent
trough php, that eventually, return�s a html page.

Here is a picture simulating this interaction.

PHP lacks a few things, such as unlike Javascript or Ajax(Browser Based


Scripting), cannot interact with the users browser(Live Data), and do thing
immediately, without the page loading.
This can be overcome, by using Ajax to send requests to another php script, and
returning data on the current, processed php script.

PHP can interact with the web server, and that is what makes it so powerful.
Unlike Javascript and other browser based scripting languages, it can send
commands, requests and a variety of other things to the web server.

Another few things that you will need to know is, is that you will definitely need
to learn a few other programming languages before you will actually be able to
write a sufficient php website/webpage.
This includes the following web programming languages.
HTML, short for Hypertext Markup Language, is the most basic of all web
programming languages, although it is very simple and easy to learn, it is one of
the most you will use.
CSS, short for Cascading Style Sheets, is a markup language that is used with/by
many programming languages, it specifies a certain look/style to an element(you
will learn what this is with html).
XHTML, short for Extensible Hypertext Markup Language, is fairly similar to HTML
but is combined with XML(Extensible Markup Language), to make it compatible with
all platforms, and all browsers.

You can go to just about any website that offers webdev tutorials and you are very
likely to see those there.
Here is a tip on getting tutorials from the web, use google, it s a very good
search engine, and if you learn how to use it correctly, you will find many good
resources.

I strongly do not recommend going on with this tutorials if you have not learned
HTML and CSS, it will be very difficult to follow with PHP if you do not know
those those languages.
The basic tutorials, you will be able to do, as it almost always, never contain
HTML elements, but once you advance, you will need it.
I also do not recommend learning HTML while Learning PHP, it may seem as a good
way to be learning(by experience), but in fact it is not, it will just get you
confused.

There are some programming languages that you will find to be good learning while
writing with it, such languages are from the SQL range.
Here are a few of them that you will most often see.
SQL, short for Structured Query Language, is a standard interactive and
programming language for getting information from and updating a database.
MySQL, is a multithreaded, multi-user SQL database management system(DBMS) which
is, according to MySQL AB, the most popular database management system.
pgSQL, short for PostgreSQL, is an object-relational database management system
(ORDBMS).

These two database management systems, are very similar in scripting, so it will
be very easy to make out, or translate/convert from and to each other.

There are allot of other DBMS�s, there are some that are even made in PHP, but it
is good to start of with one of those I mentioned above, there are a few reasons
for that.
They are very popular, so it will be very easy to find help with it, and because
it is so popular, there is allot of tutorials out there for it.

But enough with all that, you want to learn PHP at the end of this tutorial, don�t
you.
Well, to get started, you will need a PHP enabled server to start off with.
You can get a free host for this, they may not be very good, as in they have
advertisements and other things, but they will get you on the course of learning.
You can also get something such as WAMP if you have windows, alike any other
program, but it allows you to have a web server.
Feel free to google this, you will find allot of other ways, but this is a very
good way to learn how to manage your own web server as well.

Once you have a web server, with php enabled, you can continue with this tutorial,
otherwise, halt for a few minutes and do the above.

Now you will need a PHP editor, this can be a simple program such as Notepad,
WordPad or many others.
A PHP editor is simply a piece of software that helps you program PHP, almost like
an assistant, or tutor, lending a helping hand.
Many of these editors is sometimes fairly expensive, but most of these editors
does give out trails of their software.
I recommend a few, mainly phpDesigner2007, from MP Software, or Zend Studio, from
Zend.
You will need to find a editor that suites you, because most of these editors will
give false expectations, such as the following examples.
FTP Enabled, this is very handy when working in a hurry, but most, if not all,
editors actually halt(quit unexpectedly), if something goes wrong with the FTP
session.
Code Completion, also a very nifty thing, but some editors sometimes doesn�t even
bother to offer an explanation of the function, or doesn�t even have most of the
functions.

Okay, now we are equipped with all the things we will need to learn and program
PHP.

To begin a php script, or a piece of php scripting, you will need a simple line of
code.
Code:

<?php

Or just the simple and short one


Code:

<?

This is the most important part of writing a php script, if you have this
incorrect, it will display out as HTML, or give an error.
There are a few things you need to know about these opening tags, there are two
versions of it, the type you would use in an HTML page, or Javascript, and the
type where you are opening it for just a php page, although it does not really
matter which one you choose, there are two things you have to remember.
There should be no spaces, or anything before the use of the first example of the
php open tag(<?php).
The tag can also not be misspelt in any way, if you do spell it incorrectly, it
will display as HTML, because php only begins to handle php scripting one you
write a php open tag.
There is also another open tag, but mainly used to output a variable or function,
these would be used in places where a php script is in the top of the script, and
HTML or Javascript is in the lower parts of the script.
Code:

<?=$variable?>

I�m sure this seems completely vague to you, but you will understand this once you
learn about variables in php.

Lets now take the time to learn about these variables used in php.
They are very important, and is used very commonly, so it is important to pay
close attention.
Let take an example, a box, let�s say a variable is box, it does not exist, unless
you create it.
When assigning data to the variable, think of you putting something into that box.
To make it easier to identify, the box has a name assigned to it, so you know
which variable you assigned what to.
Here is an example of simply assigning a string of text to the variable.
Code:

$text = �This is a string�;

There are a few things you should have noticed, firstly, the dollar sign, this is
used to point that this is a variable, all variables in php start with this sign.
Secondly, you should have noticed the equal sign, this is also important when
assigning data to a variable.
It is simply pointing that the string next to it, is the data it must hold.
Thirdly you should have noticed the quote surrounding the string of text, this is
also important, if you do not use this, you will get output errors.
This can be either a quote or a double quote, it does not matter, only does it
matter when you use a quote or double quote within the string.
Code:

$text = �This isn�t a good string of text�;

This will give an error, as it will only see the variable�s text as being �This
isn�.
So here you will need to use double quotes, or escape the quote within the string.
Escaping any illegal characters(invalid characters), is done with a back slash,
here is an example.
Code:
$text = �This will output a quote: \��;

Here is a few things about both quotes and double quotes in php.
Single quote, this takes anything within single quotes, as literal, meaning that
anything within them will output exactly as assigned.
Double Quotes, these are not as single quotes, as it will output a variables data
if used in the string, if it isn�t it will need to be escaped using a back slash.

You can also use variables within another variable, here is an example.
Code:

$text = �This is the data of another variable: � . $another_variable;

Notice the closing single quote, as well as the dot that joins them.
The dot is used to merge, or join two or more variables, or other data types.
Also notice the underscore �_� between the words, this is mandatory, if this is
not present, and a space is used, it will produce an output error.
For more information about variables, please visit the php
website(http://www.php.net).

Next comes functions, the things you need to make a god script.
This is also a unique, well almost, thing to php, being able to create your own
functions, and using them as any other function.
There are hundreds of functions, some may make use of a lib, short for library,
there are alike packages, containing these functions source code.
Some functions, such as the GD, HTTP and some others, require their own lib to be
able to use this.
The simplest way to check if you have a lib installed is to do a php info and
manually checking.
This can be done by using the phpinfo function.
Code:

<?php
phpinfo();
?>

There are literally hundreds of functions, so now you are properly thinking to
yourself, how am I going to be able to remember them all, and remember what they
do.
After some time, you will learn, but luckily, not like many other programming
languages, php offers manuals, and references on their website.
Simply type in your browsers address bar the following.
Code:

http://www.php.net/<function>

Where �<function>� is, simply type the function you want reference on.
Many php programmers also write functions, and post comments on functions on the
php website, so you will easily know of mishaps from other people.

I will now show you some output functions that you will often use in php.
echo, this is used to output a string or variable to a page.
print, this is used alike echo, but can also be used in variables.
print_r, this is also alike the others, but can output arrays, and many other
results.
Here is an example on the first two.
Code:
echo �This is a string�;
print �This is another string�;

Alike variables, the quotes and things are used in the same way.

But here is something that I failed to mention before, the semi-colon.


This is very important, if you forget them, they will produce output errors.
They are used at the end of each function, or variable declaration.
Here is an example of an invalid piece of scripting.
Code:

echo �This is data from a variable� . $variable echo �This is another string�;

Here is the valid way it would be used.


Code:

echo �This is data from a variable� . $variable; echo �This is another string�;

Another example of valid scripting is the following.


Code:

echo �This is data from a variable� . $variable;


echo �This is another string�;

Here are some functions commonly used for encryption of data, such as passwords,
and other important data.

md5, this function cannot be decrypted, but can be cracked by comparing other
hashed(encrypted) data to them.
sha1, this is alike md5, but a bit different in encryption.
base64, this can be encrypted and decrypted, this is normally used with poor
encryptions, for less important data, such as email addresses.

Here is also a few functions used to clean unwanted html, javascript and other
scripting from a GET or POST request.

htmlspecialchars, the name says it all, it takes a html character, and get the
encoded character for it.
strip_tags, the names also gives it away, it strips php, html and other scripting
fro man string, there is also a function strip_html_tags, which only strips html
elements.
mysql_real_escape_string, this removes all mysql code from an string.
htmlentities, alike htmlspecialchars, but can be configured to encode only some
characters.

Here are a few other functions that is commonly used in php.

File system Functions


fopen, this function acts as a handler for other functions, it opens a file, in a
specific mode, read, write and acquire.
fputs and fwrite, these functions writes data to a file.
flock, this locks a file in a mode specified, so that it can only be read from, or
written to.
fclose, this closes the handler from fopen.
file_get_contents, the name also gives this functions purpose away, it gets the
data within a file specified.
file_put_contens, this function writes a string to a file specified.
Time Functions
date, this function gets a formatted date, if specified.
time, this function gets the current time.

Validation Functions
isset, this functions returns true if the string given is not empty, or null.
empty, again, this functions name gives it away, returns true if the string given
is not empty or null.
is_null, this function returns true if the given string is not null.
is_array, this function returns true if the object given is an array.
is_numerical, this function returns true if the string given is an number.
file_exists, this function returns true if the file name given exists in the
present directory.

Functions are used with �()� next to the functions name, and within the �()� comes
the parameters, also called variables.
Here is an example of making use of a function.
Code:

myFunction(�text�,$variable);

Notice that you can use a variable with the function, this is also a very useful
thing in php.
Take for instance the following, declaring a variable and using it with the
function.
Code:

$text = �A string of text�;


myFunction($text);

These will then actually then send the data of the variable to the function.
Let me now show you how to make a simple function, which will output a string of
text.
Code:

function myFunction($text) {
echo $text;
return true;
}

Notice the opening and closing brackets, they also play a vital role in php, used
in statements, functions and many other.
When stating the beginning of a function, you would use the opening bracket, �{�.
When stating the end of a function, you would use the closing bracket, �}�.
This is a common output error that occurs with the most talented of programmers,
in all languages.
This is where most php editors also play a role in programming, the software will
alert the user when they have forgotten the missing bracket, or the software will
automatically close it, it can normally be configured by the user.
Also notice the string at the end, �return true;�, this also plays a very
important role with function in particular, most programmers does not consider
this a big thing, but it can come in very handy when debugging for errors.
If a function should produce an error, you would use, �return false�, this will
then, if validating, produce the error the user used with validation.
If a function should produce a successful result, then you would use, �return
true� or �return <result>�.

If you should need further assistance with functions, please visit the php
website, they offer much other information about functions that I may have missed.

Now I will explain operators in php, they are most important, as you use them for
a variety of things throughout php.
To make it easier, I will give them all with their definitions, and then fully
explain where they would be used.

�||�, this is the �OR� operator, commonly mistaken to be only one line, and not
two.
�&&�, this is the �AND� operator, also commonly mistaken to have only one and
symbol.
�==�, this is the �Equal to� operator, also commonly mistaken to have only one
equal sign.
�!=�, this is the �Not Equal to� operator, this is commonly mistaken to have two
equal sign, when the operator with two equal sign is something else.
�!==�, this is the �Not Identical� operator, this is commonly mistaken to not
exist, but is just a rare operator to use.
�+�, this is the �Plus� operator, this is used in mathematical equations.
�-�, this is the �Minus� operator, this is also used in mathematical equations.
�*�, this is the �Multiply� operator, this is also used in mathematical equations.
�/�, this is the �Divide� operator, this is also used in mathematical equations.

Many programmers at the beginning would use �AND� and �OR� exactly as I typed, but
it is not valid, you have to use the operators, and not the text.
These operators are mostly used in if statements, which you will learn in the next
part of the tutorial.

Now it is time to learn about a very important thing in php, if statements, you
will use them a lot, so pay close attention.
If statements are very common, and they should be, they are very easy to deal
with, and very easy to understand.
Here is an example of how I remember and if statement, as an equation, �if
something, equals another, then do something, elseif another does not equal a
variable, then do something else, else do something else�, although this may seem
weird, or funny, it could help allot with remembering these statements.

Here is an example with all the statements.


Code:

if ($variable == $something) {
echo �Variable does equal something�;
}
elseif ($variable != $something && $variable == $another) {
echo �Variable does not something, but does equal another�;
}
else {
echo �Variable is not equal to anything.�;
}

Here you should notice a few things, firstly, you should notice the �()� and
within them there is a condition, almost like an equation, and if the condition is
true, the specific statement is followed.
Secondly, notice the brackets, �{� and �}�, they are important, as I told you
before, they specify the beginning of a piece of scripting, and the end.
There can be an unlimited amount of �elseif� statements, but there can only be one
�if� and �else� statement.
Another thing that makes php very good is that you can nest functions, and
specifically if statements.
Here is an example of nesting if statements.
Code:

if ($variable == $something) {
if ($variable == $something_else) {
echo �Variable is equals to something, and something else.�;
}
else {
echo �Variable is only equal to something�;
}
}
else {
echo �Variable is equal to nothing�;
}

Another thing with if statements are that you do not always have to use brackets,
�{� and �}�, if you only have one line of scripting that you want to execute, then
you do not have to use brackets with that statement.
Here is an example of how to do that.
Code:

if ($variable == $something)
echo �Variable is equal to something�;
else
echo �Variable is equal to nothing�;

You can also nest if statements the same way, take a look at this example.
Code:

if ($variable == $something)
If ($variable == $something_else)
echo �Variable is equal to something, and something else.�;
else
echo �Variable is only equal to something�;
else
echo �Variable is not equal to anything�;

You can also do equations within if statements, take a look at this example.
Code:

if ($variable == ($something+5))
echo �Variable is equal to something plus five�;
else
echo �Variable is equal to nothing�;

Notice that there are an extra set of �()�, they have to be there with equations,
if they are not present, the
sum may not be correct.

There is also another way to validate, although it is not familiar with most
programmers, it is very useful, as it can be used among and within functions.
Here is an example of that.
Code:

echo �Variable � . ($variable == $something) ? � is� : �is not� . � equal to


something�;
There are only a few things to know about this, between the �()� comes the
condition, and after the question mark �?� comes the result if the condition is
true, and after the colon �:�, comes the result if the condition is false.
It can also be used in variables, here is an example on how to do that.
Code:

$result = ($variable == $something) ? true : false;

And with all that said, it is just about everything you need to have with you to
start writing php scripts, there are allot more things to know about, but you will
eventually learn about it along the line.

Você também pode gostar