reGi’s PHP Beginner Series Part 2
String Functions
You can use strlen() to find the length of a given string, you can also use strpos() to find the location of given text in a string.
Example:
This can be used to check if a user’s password is long enough etc.
Operators
Check http://www.w3schools.com/php/php_operators.asp, I see no reason to rewrite this as it’s already been laid out their very neat and tidily.
If/Else Statements
If statements can check conditions and do different code depending on if its true or not.
Example:
If you want your If statement to execute more than one line of code if true you need to put Curly brackets ( { } ) around the code.
Example:
Switches
Switches can be used to check a strings value and execute code similar to an If statement.
Example:
Default is used if none of the cases are true. You need to use break so that the code doesn’t run into the next case.
Arrays
Arrays are used to store multiple values in one variable. Numeric arrays store values which can be referred to using the index the value is stored at. Index starts at 0. Associative arrays store values which can be referred to using an id. Multidimensional arrays can store multiple sub arrays.
Numeric Arrays Example:
Associative Arrays Example:
Multidimensional Arrays Example:
Once again, that concludes part 2 of the PHP Beginner Series. Part 3, which will probably be the last part of this series will be released soon.
Note: most code is a simplified version of W3Schools.com code, I recommend you check out their site for more PHP information.
String Functions
You can use strlen() to find the length of a given string, you can also use strpos() to find the location of given text in a string.
Example:
PHP:
<?php
echo strlen(“reGiâ€);
//Outputs 4
echo strpos(“reGiâ€, “Gâ€);
//Outputs 3
?>
This can be used to check if a user’s password is long enough etc.
Operators
Check http://www.w3schools.com/php/php_operators.asp, I see no reason to rewrite this as it’s already been laid out their very neat and tidily.
If/Else Statements
If statements can check conditions and do different code depending on if its true or not.
Example:
PHP:
<?php
$mystring = “Helloâ€;
$mystring2 = “Hello2â€;
If($mystring == $mystring2)
echo “Not Echoedâ€;
elseif($mystring!=$mystring2)
echo “Echoedâ€;
else
echo “If both previous statements are false, this will be trueâ€;
?>
If you want your If statement to execute more than one line of code if true you need to put Curly brackets ( { } ) around the code.
Example:
PHP:
<?php
$number = 1;
if($number==1)
{
echo “Number = “.$number;
$number = 2;
}
else
{
echo “Number = “.$number;
$number = 1;
}
?>
Switches
Switches can be used to check a strings value and execute code similar to an If statement.
Example:
PHP:
<?php
$number = 1;
switch ($number)
{
case 1:
echo “Number is 1â€;
break;
case 2:
echo “Number is 2â€;
break;
default:
echo “Number is not 1 or 2â€;
}
?>
Default is used if none of the cases are true. You need to use break so that the code doesn’t run into the next case.
Arrays
Arrays are used to store multiple values in one variable. Numeric arrays store values which can be referred to using the index the value is stored at. Index starts at 0. Associative arrays store values which can be referred to using an id. Multidimensional arrays can store multiple sub arrays.
Numeric Arrays Example:
PHP:
<?php
$numbers=array(“1â€, “2â€, “3â€, “4â€, “5â€);
echo $numbers[‘2’];
//Would print out 3
?>
Associative Arrays Example:
PHP:
<?php
$number[‘One’] = “1â€;
$number[‘Two’] = “2â€;
$number[‘Three’] = “3â€;
echo $number[‘Two’];
//Would print out 2
?>
Multidimensional Arrays Example:
PHP:
<?php
$items = array(“Drinksâ€=>array(“Coca Colaâ€, “Dr. Pepperâ€, “Pepsiâ€),
“Foodâ€=>array(“Pizzaâ€, “Burgerâ€, “Chipsâ€);
echo “I want to drink “.$items[‘Drinks’][0].†and eat “.$items[‘Food’][2];
//Would print “I want to drink Coca Cola and eat Chips
?>
Once again, that concludes part 2 of the PHP Beginner Series. Part 3, which will probably be the last part of this series will be released soon.
Note: most code is a simplified version of W3Schools.com code, I recommend you check out their site for more PHP information.