PHP Basics
PHP Basics
PHP
It is an open-source, cross-platform,
interpreted, and server-side scripting language
that is used to create web applications/sites
Written by Rasmus Lerdorf and originally
known as Personal Homepage/Forms
Interpreter
Now stands for the recursive acronym “PHP:
Hypertext Preprocessor “
CLIENT SIDE VS SERVER SIDE SCRIPTING
Client-Side Server-Side
1. Executed on the client 1. Runs on a web server.
side.
2. User has access to all the 2. Source code is hidden.
code received by the client.
3. Do not require additional 3. Require that their
software on the server language's interpreter be
installed on the server
What do I need to run my PHP script?
Linux or Windows or any OS
- Linux provides the base operating system (OS) and server environment
Apache or IIS (HTTP Server)
The Apache web server intercepts HTTP request and either serves them directly
or passes them on to the PHP interpreter for execution
MySQL
The MySQL RDBMS serves as the data storage engine, accepting connections
from the PHP layer and inserting, modifying, or retrieving data
<?php
echo "Hello World!";
?>
</body>
</html>
INSERT PHP IN HTML TAGS
Four Rules in Coding
1. Every block of PHP starts with <?php and ends
with ?>
2. PHP statements end with a semicolon (;)
3. Text that is not a command must be put in quotes
4. Any time you have a curly bracket or parenthesis,
you should have a matching end bracket or
parenthesis.
EMBEDDING PHP WITHIN HTML
<!DOCTYPE html>
You can insert the PHP scripts in any portions
<html> of the HTML tags. Also you can embed HTML
<head><title>My First PHP Code</title> tags in PHP statements however it will be
</head> treated as text.
<body>
<h2>This creature can change color or to blend in with its surrounding. What is its name?
</h2>
<?php
//display output
echo “<h2>A Chameleon</h2>”;
?>
</body>
</html>
COMMENTS
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
</body
</html>
PHP: VARIABLE, DATA TYPES, OPERATORS,
CONDITIONS AND ITERATION
VARIABLE
In PHP, a variable starts with the $ sign and followed by the name of the variable;
It can have a short name or a more descriptive
Rules for PHP variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and
_)
Variable names are case-sensitive ($age and $AGE are two different variables)
<?php
?>
SINGLE VS DOUBLE QUOTES
' ' (Single Quotes)
Content inside single quotes is evaluated literally.
Example
$string = 'Single Quotes';
echo '$string';
<?php
$d1 = new DateTime("2013-12-09");
$d2 = new DateTime("2014-03-17");
$difference=$d1->diff($d2)->days;
if ($difference<100){
echo 100-$difference." left before 100 days";}
?>
IF..ELSE STATEMENT
• Syntax
if (expression) {
// code to execute if the expression evaluates to true
} else {
// code to execute in all other cases
}
<?php
$mood = "sad";
if ($mood == "happy") {
echo " Yehey, Masaya ako nasa mood ako";
}
else {
echo "Di Masaya kaya malungkot";
}
?>
IF..ELSEIF STATEMENT
• Syntax
if (expression) {
// code to execute if the expression evaluates to true
} else if (another expression) {
// code to execute if the previous expression failed
// and this one evaluates to true
} else {
// code to execute in all other cases
}
<?php
$mood = "sad";
if ($mood == "happy") {
echo "Yehey, Masaya ako nasa mood ako";
} elseif ($mood == "sad") {
echo "Di Masaya kaya malungkot”;
} else {
echo "Masaya b o malungkot?";
}
?>
SWITCH STATEMENT
• Syntax
switch (expression) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement
// has been encountered hitherto
}
<?php
$mood = "sad";
switch ($mood) {
case "happy":
echo "Yehey, Masaya ako nansa mood ako";
break;
case "sad":
echo " Di Masaya kaya malungkot”;
break;
default:
echo "Masaya b o malungkot?";
break;
}
?>
LOOPING STATEMENT
WHILE STATEMENT
• Syntax
while (expression) {
// do something
}
<?php
$counter = 1;
$total=0;
while ($counter <= 12) {
echo "$counter times 2 is ".($counter * 2)."<br/>";
$total+=$counter*2;
$counter++;
}
echo “The total of the numbers is ”. $total;
?>
WHILE STATEMENT
• Syntax
while (expression) {
// do something
}
<?php
$counter = 1;
$total=0;
while ($counter <= 12) {
echo "$counter times 2 is ".($counter * 2)."<br/>";
$total+=$counter*2;
$counter++;
}
echo “The total of the numbers is ”. $total;
?>
DO..WHILE STATEMENT
• Syntax
do {
// do something
}while (expression);
<?php
$num = 1;
do {
echo "The number is: $num<br>";
$num++;
} while (($num > 200) && ($num < 400));?>
FOR..LOOP STATEMENT
• Syntax
for (initialization expression; test expression; modification
expression) {
// code to be executed
}
<?php
for ($counter=1; $counter<=12; $counter++) {
echo "$counter times 2 is ".($counter * 2)."<br/>";
}
?>
REFERENCES
http://localhost/form.php?name=Peter&age=37
$_POST PREDEFINED VARIABLE
The $_POST variable is an array of variable
names and values sent by the HTTP POST
method.
The $_POST variable is used to collect values
from a form with method="post".
Information sent from a form with the POST
method is invisible to others and has no limits
on the amount of information to send.
$_REQUEST PREDEFINED VARIABLE
The PHP $_REQUEST variable contains the
contents of both GET, POST methods.
DEALING WITH MULTIVALUE FIELD
Example
<input type= "checkbox" name=“number[]" value= "1" />1
<input type= "checkbox" name=“number[]" value= "2" />2
<input type= "checkbox" name=“number[]" value= "3" />3
STORING PHP VARIABLES IN FORMS
A hidden field is a special type of input element
that can store and send a string value, however, it
is not displayed, its value cannot be change by
the user;
Example
$name="John Smith";
<input type="hidden" name="name"
value="<?php echo $name?> " >
REDIRECTING AFTER A FORM SUBMISSION
Example
header( “Location: thanks.html”);
header('Refresh: 10; URL=home.php');