PHP basics tutorial
Ranked #1,589 in Internet, #92,988 overall
Get your hands on the basics of PHP within a few minutes!
This tutorial was made for people who are totally new to programming in general ( might already know HTML, not necessarily ) and want to start off learning PHP right away with a basics tutorial that's easy to grasp. There's no requirements for this tutorial as for pre-knowledge at all. It's for the total newcomers to PHP programming and will discover the pure basics of programming in PHP.
Talked enough, let's program! ;)
What is PHP?
What is it, how and wherefor can I use it?
1. Download XAMPP.
2. Install it.
3. Run XAMPP (\installation\path\xampp-control.exe (for ex.: C:\xampp\xampp-control.exe)).
4. Start Apache (under Modules).
And you're ready to go! You can now run any PHP file that's placed inside of the directory installation\path\htdocs (for ex.: C:\xampp\htdocs).
Note: As mentioned above, the alternative to an apache server on your personal computer would be to find a free webhosting that runs PHP codes ( there are plenty out there ), for example 000webhost.com.
Time to program!
Creating your first very small PHP script!
We'll start with creating a "Hello World!" webpage ( original, no? ).
First create the PHP file, call it: test.php and put it inside the htdocs directory of your XAMPP installation folder ( e.g. C:\xampp\htdocs ). Do make sure the .php inside of the filename is used as extension for the file, and not as name ( e.g. not test.php.txt but test.php ). Now open it with notepad and write the following inside of the file ( will explain in a second ):
<?php
echo "Hello World!";
?>
Hello World!
Ain't it amazing? ... ok it may not be that amazing, but it's a start right?So, you may already have realized that http://localhost refers to the htdocs directory of your XAMPP installation folder (e.g. C:\xampp\htdocs) and so we can go to http://localhost/path/to/file to access any PHP files we made inside of this directory ( including the test.php we just made ) and the web browser will execute the PHP script. Now let's have a look inside of the PHP script we just made. It started with some weird tag <?php and ended with ?>. These (so called) tags indicate the start and end of a PHP script. This way the browser knows that what we're programming in is PHP ( so it knows how to convert/compile the code ). These tags are required for a PHP code to work. Always start a PHP code with . Also note that inbetween of these tags you may only write PHP codes ( no normal text or other codes than PHP ).
Then there's the following line:
echo "Hello World!";
You might already have guessed that this is the part that writes the text "Hello World!" on the webpage. This is exactly what the echo function does. It's an PHP built-in function that is used to display text on a webpage. The only parameter the function needs is the text to display ( in this case "Hello World"). In functions in PHP, this parameter ( the thing the function needs to do its job ) is put behind the function name ( in this case echo ) and if it's text, it's put between quotes. However, for normal functions these parameter(s) should be put between brackets. For the built-in echo function this is not necessary, but you may also write:echo("Hello World!");
for if you like. Another function that does exactly the same is the print function, however it tends to be slower so I personally prefer the echo function above the print function.print("Hello World!");
Also note that each sentence in PHP ends with a ; symbol. This is strictly required for each sentence to end with, otherwise the webpage will throw out a parse error. So take good note of that if you don't want to end up in loads of parse errors!That's it, your first very simple webpage in PHP displaying text, has been created successfully! Next is: creating a PHP code using variables. These are a vital part of programming in PHP and you'll need to use them a lot if you're planning on creating PHP systems ( which I assume you are ).
Using variables in PHP
What are variables and how to use them in my PHP script?
So let's see how a variable is created in PHP. As mentioned above it consists of a name and a value, these are binded through a = symbol and the name is preceded by a $ to indicate that it's a variable:
$variable_name = variable_value;
The variable name may not consist of spaces or any other special characters other than an undercore ( for the rest: alphabetical characters and numbers ). The value can be anything: a string ( text ), a number, a boolean (true or false value), anything. Do note that strings should be put between quotes, and numbers should not:$string = "some text";
$number = 10;
echo $variable_name;
We can use the echo value which we learnt to display text on a webpage. We can also make calculations with variables, for example try and run this code:<?php
$num = 1;
echo "num = ".$num." .";
$num = $num+3;
echo "num = ".$num." .";
$num = ($num+1)*2;
echo "num = ".$num." .";
?>
num = 1. num=4. num=10.
The normal mathematics rules count here. You may notice that we put the text between quotes and then seperated the variable from the text by ending the quotes and putting a dot. This is necessary because variables should ( usually ) not be put inside of quotes and should therefore be seperated from the quotes. This is done by ending the quotes and putting a dot before the variable. To continue the text after the variable, we put a dot behind the variable and reopen the quotes. It's also possible ( when using double quotes and no array deminsial variables ) to seperate the variable from the text by using accolades, like this:echo "this is the value of a variable: {$variable_name} -cool.";
Also, the calculations we done can be written faster ( some of them ). The following calculations:$num = $num+3;
$num = $num*2;
$num += 3;
$num *= 2;
Note: When you want to print a variable literally ( instead of the value of the variable ), then the dollar sign should be escaped by putting a backslash before the sign. This way the sign is treated as plain text, and not as a sign that indicates the start of a variable.
Using loops in PHP
What are loops and what can they be used for?
if( condition ) {
executable code
}
<?php
$secret_number = 79;
$guessed_number = 62;
if($guessed_number == $secret_number) {
echo "You guessed the number correctly!";
}else{
echo "I'm sorry but {$guessed_number} is not the secret number.";
}
?>
I'm sorry but 62 is not the secret number.
The accolades can as well be left out but only for single line executable codes!
The while, do-while and for loop
Another much used loop in PHP is the while loop. This loop can be used to repeatively execute a code as long as a condition is true.
while( condition ) {
executable code
}
do {
executable code
} while ( condition );
<?php
$i = 1;
while($i <= 10) {
echo " {$i} ";
$i++;
}
?>
1 2 3 4 5 6 7 8 9 10
We can also use the for loop to do this. Which is more restricted to execute a certain code for a given amount of times and is structured like this:for( set init value; condition; action ) {
executable code
}
for($i=1;$i<=10;$i++) {
echo " {$i} ";
}
Do note the difference between the while and for loop: the for loop needs a specific amount of times to repeat to be set and the while loop does not and can therefore be used more diverse.
Comparison & Logical Operators
Operators to use inside conditions of your loops.
Comparison Operators
These are used to actually make comparisons.
Operator
Meaning
==
equal to
!= or <>
not equal to
<=
smaller or equal to
>=
greater or equal to
Logical Operators
These are used to combine comparisons ( for ex.: comparison 1 needs to be true AND comparison 2 should not be true ).
Operator
Meaning
AND or &&
and
OR or ||
or
Note: Another useful operator can be the ! sign, which means litterally NOT. It can invert a comparison, for example:
if(!($foo == $bar AND $x > $y))
which is equal to the comparison:
if($foo != $bar AND $x <= $y)
You might also like ...
Next PHP Tutorial
Comments & Questions
Got any questions about the tutorial or just want to comment? This is the place to do so.
-
-
Ram
Oct 2, 2011 @ 1:19 am | delete
- hi! I am new to php, your lens help me lot and thanks
Regards,
ram
-
-
-
dogface
Aug 19, 2011 @ 12:45 pm | delete
- Wonderful lens! Oh, and I already knew everything by the way. ;-)
-
-
-
edzsquid
Aug 4, 2011 @ 4:26 am | delete
- Great lens! I will wait for your next lens!
-
-
-
corrytohsan
Jul 10, 2011 @ 9:37 am | delete
- Thanks it's help me a lot.
-
-
-
RecnepsDaGreat Jun 21, 2011 @ 7:48 pm | delete
- Great Lens! I haven't ever gotten into PHP but I have done a little programming. I think your bio is hilarious. It's so true
-
- Load More
Related Products (Amazon)
Related Products (eBay)
by webcodez
"Give a man a program and you'll frustrate him for one day. Teach a man to program and you'll frustrate him for a whole lifetime."
webcodez
GameHeroes
more »
- 21 featured lenses
- Winner of 14 trophies!
- Top lens » PHP & MySQL Poll System Tutorial
Explore related pages
- MySQL Basics Tutorial MySQL Basics Tutorial
- HTML Basics Tutorial HTML Basics Tutorial
- PHP Functions And Classes PHP Functions And Classes
- PHP Sessions and Cookies Tutorial PHP Sessions and Cookies Tutorial
- PHP Arrays Tutorial PHP Arrays Tutorial
- PHP Including Files And Securing Included Files Tutorial PHP Including Files And Securing Included Files Tutorial