Chapter 1_Php Language
Chapter 1_Php Language
Web Programming
PHP Language
NORULMUBARAKAH IBRAHIM
TTO ROOM:L10 08
TTO, Industrial Electronics
German Malaysian Institute
Objectives
Upon completion of this subject, the students
should be able to:
Understand the process of Client and Server
Understand the working model of HTML and
PHP
Create your own web site make connection
to database
Course Assessment
Test
Test 1 - 10%
Test 2 – 10%
Practical test 3 – 10%
End Module Test
Group Assig. 30%
PBL 1 - 15%
PBL 2 - 15% Attitude
10%
Concept of CLIENT and SERVER
PHP Code:
<?Php
……
?>
Example of simple PHP script
Each code line in PHP must
end with a semicolon;
to output text with PHP: echo
<?php
echo "Hello World"; or print
?>
PHP Variables
Variables are used for storing a values, like text
strings, numbers or arrays.
When a variable is set it can be used over and
over again in your script
All variables in PHP start with a $ sign symbol.
The correct way of setting a variable in PHP:
$var_name = value;
Variables…cont
creating a variable with a string and
number.
a variable does not need to be declared
<?php before being set.
$txt = "Hello World!"; PHP automatically converts the variable
$number = 16;
to the correct data type.
?>
Variable Naming Rules
must start with a letter or an underscore "_"
can only contain alpha-numeric characters and
underscores (a-Z, 0-9, and _ )
should not contain spaces. If a variable name is more
than one word, it should be separated with
underscore ($my_string), or with capitalization
($myString)
String in Variables
A string variable is used to store and manipulate a
piece of text.
String variables are used for values that contains
character strings.
Below, the PHP script assigns the string "Hello
World" to a string variable called $txt:
Output:
<?php
$txt="Hello World"; Hello World
echo $txt;
?>
The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string
values together.
To concatenate two variables together, use the dot (.)
operator:
<?php Output:
$txt1="Hello World";
$txt2=1234; Hello World 1234
echo $txt1 . " " . $txt2;
?>
Task-sample output
NorulMubarakah
Thank you.
PHP Strings
Output:
<?php
echo strlen("Hello world!"); 12
?>
Using the strpos() function
The strpos() function is used to search for a string or
character within a string.
If a match is found in the string, this function will return
the position of the first match. If no match is found, it will
return FALSE.
Let's see if we can find the string "world" in our string:
Output:
<?php
echo strpos("Hello world!“,”world”); 6
?>
PHP Operators
PHP Operators…cont
PHP If...Else Statement
Conditional Statements
Very often when you write code, you want to perform
different actions for different decisions.
You can use conditional statements in your code to do
this.
if...else statement - use this statement if you want to
execute a set of code when a condition is true and
another if the condition is not true
elseif statement - used with the if...else statement to
execute a set of code if one of several condition are
true
If…Else Statement…cont
The If...Else Statement
If you want to execute some code if a condition is true
and another code if a condition is false, use the
if....else statement.
Syntax Example
<?php
$d=“Fri”;
If (condition) if ($d=="Fri")
code to be execute if condition is true; echo "Have a nice weekend!";
else
else echo "Have a nice day!";
Code to be execute if condition is false; ?>
The ElseIf Statement
If you want to execute some code if one of
several conditions are true use the elseif
statement
Syntax Example
If (condition) <?php
code to be execute if condition is true; $d="sun";
Elseif (condition) if ($d=="Fri")
code to be execute if condition is true; echo "Have a nice weekend!";
Elsels elseif ($d==“sun”)
code to be execute if condition is false; echo "Have a nice sunday!";
else
Echo “have a nice day”;
?>
The Switch Statement
If you want to select one of many blocks of code to be
executed, use the Switch statement.
The switch statement is used to avoid long blocks of
if..elseif..else code.
switch (expression)
{
Syntax
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed if expression is different from both label1 and label2; }
Switch Statement…cont
echo "Traveling to $destination<br />";
switch ($destination)
Example1 { The value of $destination
case "Las Vegas":
echo "Bring an extra $500";
was Tokyo.
break; The switch operating
case "Amsterdam":
echo "Bring an open mind"; search for a case that
break; match a value.
case "Egypt":
echo "Bring 15 bottles of SPF 50 Sunscreen"; A Break;to prevent the
break; other cases is being
case "Tokyo": execute
echo "Bring lots of money";
break;
case "Caribbean Islands":
echo "Bring a swimsuit";
break;
Output
Default: echo “Rest at home”;
Traveling to Tokyo
} Bring lots of money
PHP Arrays
An array stores multiple values in
one single variable
An array is a special variable, which
can hold more than one value at a
time.
An array can hold many values under
a single name, and you can access
the values by referring to an index
number.
Create an Array in PHP
In PHP, the array() function is used to create an
array.
there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing
one or more arrays
Numeric Arrays
A numeric array stores each element with a numeric ID
key.
A numeric array stores each element with a numeric
ID key.
There are different ways to create a numeric array.
Example 1 Example 2
in this example the ID key In this example we assign
is automatically assigned: the ID key manually:
$names[0] = “Peter”;
$names=array(“Peter”,”John”,”Joe”); $names[1] = ”John”;
$names[2] =” Joe”;
Numeric Arrays
Output:
Quagmire and Joe are Peter's neighbors
Associative Arrays
each ID key is associated with a value.
When storing data about specific named values, a numerical
array is not always the best way to do it.
With associative arrays we can use the values as keys and
assign values to them.
Example1
Example2
The different way of= creating
$ages['Peter'] "32"; an array
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
Associative Array
The ID keys can be used in a script:
<?php $ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
Example1 <?php
$i=0;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
The do...while Statement
The do...while statement will execute a block of code at
least once - it then will repeat the loop as long as a
condition is true.
The following example will increment the value of i at
least once, and it will continue incrementing the variable i
as long as it has a value of less than 5:
Syntax do { code to be executed; <?php
} $x = 1;
while (condition); do {
echo "The number is: $x <br>";
$x++;
Example1 } while ($x <= 5);
?>
The for Statement
The for statement is used when you know how
many times you want to execute a statement or
a list of statements.
Example1 <?php
Syntax for ($i=0; $i<=5; $i++)
{
for (initialization; condition; increment) echo "Hello World!<br />";
{ }
code to be executed; ?>
}
PHP Functions
Create a PHP Function
A function is a block of code that can be
executed whenever we need it.
Creating PHP functions:
All functions start with the word "function()"
Name the function - It should be possible to
understand what the function does by its
name. The name can start with a letter or
underscore (not a number)
Add a "{" and "}"
Example of function
A simple function that writes my name
when it is called: <html>
<body>
Example2
<?php
<html> function writeMyName()
Example1 <body> {
<?php echo "Kai Jim Refsnes";
function writeMyName() }
{ echo "Hello world!<br />";
echo "Kai Jim Refsnes"; echo "My name is ";
writeMyName();
}
echo ".<br />That's right, ";
writeMyName();
writeMyName();
?> echo " is my name.";
</body> ?>
</html> </body>
</html>
Function…Adding parameter
Example1
<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
</body>
</html>
PHP Functions
Functions can also be used to return values.
Example1 <html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
PHP Forms and User Input
The PHP $_GET and $_POST variables are
used to retrieve information from forms, like user
input.
PHP Form Handling
The most important thing to notice when dealing with
HTML forms and PHP is that any form element in a
HTML page will automatically be available to your
PHP scripts.
PHP Forms and User Input
Example1: welcome.html
http://localhost/welcome.php http://localhost/welcome.php?
name=Peter&age=37
The $_REQUEST Variable
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
PHP Sessions
A session is a way to store information (in
variables) to be used across multiple
pages.
Unlike a cookie, the information is not
stored on the users computer.
Session stored user information to be used
across multiple pages (e.g. username,
favorite color, etc). By default, session
variables last until the user closes the
browser.
Start a PHP Session
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
Session also can be modify and destroy
Use session_destroy(); to
destroy session