Beginning PHPHello! In this tutorial, I'll show you how to start learning PHP. By now, you should have some basic knowlege of HTML/XHTML.
Get StartedPHP files end with *.php. However, to run these files in a browser, you need to set up a localhost. I did this by installing XAMPP from
here. Once you have it installed, navigate to C:/xampp/htdocs. Place a test file called test.html and pull up your browser. Go to
http://localhost/test.html and you'll find your file! Success, XAMPP is installed.
NOTE: By installing XAMPP, you allow users to navigate there by knowing your IP address. To stop this, go to http://localhost/ and click on "Security" then follow the instructions.Your first PHPEveryone starts with the "Hello World" application, lets follow tradition! Save this as helloworld.php:
CODE
<html>
<head>
<title>Hello!</title>
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Now you must be wondering why we used HTML elements in a PHP file. Run the file at
http://localhost/helloworld.php . You should see "Hello World" on the screen. Now, to see the advantage of PHP, view the source:
CODE
<html>
<head>
<title>Hello!</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
There is the advantage of PHP! Since PHP is a server-side language, it processes the code before the client sees it. This is why PHP is so widely used in secure systems.
Taking the code apart, the
<?php starts PHP code, the
echo " "; command prints a line, and the
?> code closes the PHP code.
NOTE: PHP code will not work with the *.html or *.htm extension. A PHP file must have the *.php extension.VariablesAs you would know if you know JavaScript, variables are
"code that can vary". Use them to work with loops, if-else's, and others. Variables can also keep you from typing code over and over. PHP variables are declared like:
CODE
<?php
$varName = value;
?>
When calling a variable, use:
CODE
<?php
$var1 = "Hello";
$var2 = "World";
$total = $var1 . " " . $var2;
?>
Pay close attention to $total and you'll notice the period that adds the strings. This is called a concentration operator and adds two or more strings. Also see how we called $var1 and $var2 with a $ symbol just like how we declared them.
Another form of our "Hello World" app:
CODE
<?php
$hello = "Hello";
$world = "World";
$fullLine = $hello . " " . $world;
echo "$fullLine";
?>
CommentsComments in PHP are written just like in java script:
CODE
/*This is a
multi-line
comment */
// This is a single line comment
# So is this
Comments are not executed nor seen by the processor. They are for your eyes only!
OperatorsOperators are what shuffle and add and subtract variables, strings, etc. A list of operators:
- = : Assignment
- + : Addition
- - : Subtraction
- * : Multiplication
- / : Modulus
- . : String concentration
- == : Equal to
- === : Equal to and of same type
- <> or != : Not equal to
- !== : Not equal to; not same type
- < : Less than
- <= : Less than or equal to
- > : Greater than
- >= : Greater than or equal to
- && : Logical AND
- || : Logical OR
- xor : Logical XOR
- ! : Logical NOT
- ++ : Add 1
- -- : Subtract 1
Some of these operators you will have seen in math such as "+" or "-". Others might be unfamiliar. Operators will come in handy in the next lesson:
If...ElseIf else is the most used statement in just about all code. And example is shown:
CODE
<?php
$number = 10;
if ($number == 10) {
echo "Number is 10";
} else {
echo "Number is not 10"
}
?>
See how we used { and } to define what the commands should do if the tests returned true or false. Now is where variables come into handy. Say you have input from a user. You can assign that input to a variable and run tests on the variable.
Data from formsThis is the most advanced idea in the whole tutorial so fear not! Lets say you have a form in HTML
CODE
<html>
<body>
<form method="post" action="actionhere">
<input type="text" name="name" />
<input type="submit" />
</form>
</body>
</html>
See how the form element has an action=" parameter? Fill this in:
CODE
<form method="post" action="action.php">
This will redirect the user to "action.php" when the user clicks submit. To get information from a form, we use:
CODE
<html>
<body>
<?php
$name = $_POST["name"];
echo "Your name is: " . $name;
?>
</body>
</html>
Here we used $_POST["input"] to grab the input from the form. Then we assigned it to a variable and wrote it to the screen. You can also us $_GET if the method of the form was get. Get passes the input through the address bar and can be handy if the user want to bookmark the page.
?>Ok, hopefull, I've given you the basics of PHP! Go have fun!
Instalation: I take no responsibilty for what you download here or on apache friends. That is your decision.