M'kay, that's better then.
PHP: BeginnersTo start off, let's explain exactly what PHP is.
PHP: PHP (PHP: Hypertext Preprocessor) used to stand for Personal Home Page, was created by Rasmus Lerdorf in 1994. It was a set of Perl Scripts to track visitors to his site. Later, he revised it with a Form Interpreter (FI) and released it again in 1996 as PHP-FI. Two people named Zeev Suraski and Andi Gutmans made the API, the PHP parser that became PHP3 in 1998. It was later updated as PHP4 with the Zend Engine to allow for scripting to be used with any combination of Web Server, Browser, and OS.
What it does: PHP a server side web language. Server side languages are run on the web server, so it is OS universal, and you don't need to download anything to run it. PHP uses commands to alter the file that the web server sends to a client computer before it is sent. This way, ceratin things, like data in a file, or the time, can end up showing up different things.
Getting Started:--1.0: Basics and Echos.CODE
<?
echo(" Hello World ");
?>
This is a really simple piece of code. To start off PHP code, you have to use
<?,
<?php, or
<script language="php">. The most common used is <? or <?php.
The function
echo(); will display text on the page. There's a few things you have to know about it.
If you use echo(" Bob said "RAWR"! ");, it will NOT work. There are four "'s in there. It is reading it as Opening, closing, opening, and closing, which cannot happen in a function. There's two ways around this.
echo(" Bob said \"RAWR\"! ");
or
echo(' Bob said "RAWR"! );
The first makes the inner two quotes escape, they aren't read as closing the function. The second uses single quotes, so the double quotes don't close the function.
Remember, that ALL functions must be closed with a semi-colon ( ; ).
--1.1: Variables.CODE
<?
$variable = "rawr";
echo(" Bob said $variable ");
?>
Variables are much easier in php then most languages. In C++, you have to declare the variable type too, but in PHP, you don't. Variables also don't have to be decalred, and can switch between types.
To set a variable, use $(Variable Name) = "Thing to set it to"; You can set them to strings ("Hi there, my name is bob!"), numbers (1, 2, 3), booleans (TRUE, FALSE), or floating points (3.1246135).
A few things about variable names. You can not use a variable that has any of
these names. Also, no name can start with a number ($1rawr) and none can have spaces ($rawr rawr).
There's a few ways to echo a variable:
echo(" Variable: {$variable} ");
echo(" Variable: $variable "):
echo(' Varaible: '.$variable.' ');
Variables are case sensitive, meaning $RAWR and $rawr are NOT the same.
--1.2 Comments.CODE
<?
echo("Hello World");
//This wont be parsed.
#Neither will this.
/*Or this,
Or this*/
?>
Comments can be declared with #, //, or /* */. # and // are single line comments. Anything you write until you hit enter wont be parsed. /* */ are multi line comments. Until you type a */ after a /*, nothing of that will be parsed.
--1.3 Operators-Arithmetical Operators
CODE
$sum = $one + $two;
$difference = $one - $two;
$product = $one * $two;
$quotient = $one / $two;
$modulus = $one % $two;
$increment = $one++;
$decrement = $two--;
These are simple. Adding, subtracting, multiplying, deviding, multiplying, increment, decrement.
Modulus divides the numbers, and return the remainder of the operation.
Increment and decrement add or subract one.
-Logical Operators
CODE
&& / AND
|| / OR
XOR
!
&& and AND evaluate the the operands and return true only if BOTH operatives are true.
|| and OR evaluate the two operands and return true only if only one, or both are true.
XOR evaluates the operators and returns true only if ONE of the operands returns true.
! (NOT) evaluates the operands and returns true only if BOTH of the operands are false.
-Assignment operators
CODE
= ($a = $b)
+= ($a += $b)
.= ($a .+ $b)
-= ($a -= $b)
*= ($a *= $b)
/= ($a /= $b)
%= ($a %= $b)
The first two, (Skipping =, we know what that does (hopefully
)) += and .= are adding operators.
+= is used for numbers, it is equivalent to ($a = $a + $b).
.= is used for strings. It will attach the second string to the end of the first.
-= is equal to ($a = $a - $b)
I think you should get it by now
-Comparison Operators
CODE
if ($a == $b)
if ($a != $b)
if ($a > $b)
if ($a < $b)
if ($a >= $b)
if ($a <= $b)
The
if function tests the operands, and preforms the operations if true is returned.
== checks if the operands are equal.
!= checks if the operands are not equal.
> checks if the first operand is greater than the second one.
< checks if the first operand is less than the second one.
>= checks if the first operand is greater than or qual to the second one.
<= checks if the first operand is less than or equal to the second one.
Remember to use == and not = when checking if two variables are equal.
--1.4 Functions.CODE
<?
function line()
{
echo("<hr />");
}
function add( $arg, $arg2 )
{
$sum = $arg + $arg2;
return $sum;
}
line();
$sum = add(1, 2);
echo($sum);
line();
?>
Functions are set, then called. Whatever things are in the function are executed everytime the function is called.
If you give the function arguments, (function add), then it will modify the arguments, Use the
return statement to return a variable (For $a = function()
.
--1.5 Statements.-If Statement
CODE
$a = 1;
$b = 1;
if($a == $b)
{
echo("They're Equal ^_______________________^");
}
This is simple. It checks the two variables (In this case, if they are equal) and if they are, it preforms the operation (In this case, the echo function).
-If-Else and Else
CODE
$a = 1;
$b = 2;
if($a > $b)
{
echo("Variable a is greater than variable b");
}
elseif($a < $b)
{
echo("Variable b is greater than variable a");
}
else
{
echo("Dude, that's messed up....");
}
The
elseif is used after an if statement to provide alternative code if the if statement is proven false. The
else (Elseif without evaluation) function is used after an if or elseif function, and provides code if NONE of the if/elseif functions are proven true.
-Switch Statement
CODE
$num = 2;
switch($num)
{
case 1 : echo("Variable equals 1"); break;
case 2 : echo("Variable equals 2"); break;
default : echo("This shouldn't happen");
}
The
switch statement is used to evaulate a variable against several different things at once. It's like compacting an ifelse statement. If checks whatever is after the
case, and if a match is found, preforms all the functions after the colon. Always add a
break; after the functions, or else every case will execute. The
default case is used if no match is found.
-While Loop
CODE
<?
$i = 0;
$num = 50;
while($i < 10)
{
$num--;
$i++;
}
echo("Loop ended at {$i}, \n \$num is now $num");
?>
A
while loop will continue to loop until the statement is proven false. Every statement inside will be executed as it goes.
-Do-While Loop
CODE
$i = 0;
$num - 50;
do
{
$num--;
$i++;
}
while($i<1);
echo("Loop stopped at {$i}, \n \$num is now $num");
This is the same as a while loop, except the statements are executed before the check statement. So the functions will ALWAYS be executed once.
-For loop
CODE
$a = 0;
$b = 0;
for( $i=0; $i<5; $i++ )
{
$a += 10; $b += 5;
}
echo("At the end of the loop, a = $a and b = $b");
A
for loop is usually the most used loop. This is the syntax
for( INITIALIZER; TEST; INCREMENT).
Where initializer is what the variable starts at, test is what the for loop tests, and increment is how much the variable goes up.
-Interupting and Continuing loops
CODE
break;
continue;
Break and
Continue are used to stop and start loops. I've never used continue, so I really don't know much about it, but the break statement can be used to like, make sure a loop only loops five times, ect.
These are some basic functions for PHP. I'll post some more advanced tutorials later. If anyone has any questions, feel free to ask them here
EDIT: Someone needs to fix the addtion part of this, so I don't have to post 20 tutorials per post. Maybe add something in the tutorials database for these, or enable double posting in this forum?