Professional Documents
Culture Documents
There are two ways in which you can add comments to your PHP code
Single-line comments
<?php
//this is a comment
?>
Multiple-line comments
<?php
/* This is a section
of multiple comments*/
?>
Basic Syntax
String variables
The quotation marks indicate that is a string
$username = “Fred Smith”;
Numeric variables
Variables contain numbers
$count = 17; (integer)
$count = 17.5; (floating point)
Arrays
When creating PHP variables, you must follow these four rules
Variable names must start with a letter of the alphabet or the _ (underscore)
Variable names can contain only characters: a-z, A-Z, 0-9, and _ (underscore)
Variable names may not contain spaces.
Variable names are case-sensitive
Activity: Variable declaration
Operators
PHP supports two types of strings that are denoted by the type of quotation mark that you
use
If you wish to assign a literal string, preserving the exact contents, you should use single
quotation mark
If you would like PHP to include the value of a variable inside string, you should use double
quotation mark
Example:
$count = 5;
echo ‘There are $count msgs’; //There are $count msgs
echo “There are $count msgs”;//There are 5 msgs
Escaping Characters
Escaping Characters
Special meanings that might be interpreted incorrectly
E.g., $text = ‘My sister’s car is Ford’;//Error
Must be: $text = ‘My sister\’s car is Ford’;
There are characters like: \t, \n, \r
The \ itself should be \\
Activity: String types and escaping
Variable Typing
Besides echo, there is another way to output text to browser, the print command
print is the actual function that takes single parameter
echo is a PHP language construct, it is faster than print since it is not a function and doesn’t return
value
Example
$b ? print “TRUE” : print “FALSE”; //correct
$b ? echo “TRUE” : echo “FALSE”; //error
Functions
Functions are used to separate out sections of code that perform a particular task
Reusability (code once, use everywhere)
For maintainability (changes in one place)
For readability (clarity of your code)
Example
<?php
function longdate($timestamp){
return date(“l F jS Y”, $timestamp);
}
echo longdate(time());
?>
You can have a reference to the complete list of date formats from here:
http://php.net/manual/en/function.date.php
Activity: Functions
Global variables
To use global variables at the global scope inside a function we need to declare
these explicitly.
Superglobal Variables
}
?>
The if…else statement
The else Statement
When the conditional is not TRUE, you may wish to do something else instead
This is where else statement comes in
Example
<?php
if($bank_balance < 100){
$money = 1000;
$bank_balance += $money;
}else{
$savings += 50;
$bank_balance -= 50;
}
?>
The elseif Statement
}else{
$savings += 50;
$bank_balance -= 50;
}
?>
The switch Statement
}
?>
do…while Loops
The block of code is executed at least once before checking the condition
Example
<?php
$count =1;
do{
echo “$count times 12 is “. $count * 12 . “<br/>”;
$count++;
}
?>
break out of a loop
Break statement will allow to break out of the loop when called
Example:
<?php
for($i = 0; $i < 10; $i++){
if($i == 5){
break;
}
echo $i . “<br/>”;
}
?>
continue Statement
Allowing PHP to skip everything after this statement in loop block and continue to the next
iteration
<?php
for($i = 0; $i < 10; $i++){
if($i == 5){
continue;
}
echo $i . “<br/>”;
}
?>
Implicit and Explicit Casting
Nixon, R., 2014. Learning PHP, MySQL & JavaScript. 4th ed. Oreilly.