Chapter 4 Introducing PHP Setting Up a Web Server PHP Basic Syntax PHP Variables PHP Operators PHP Flow Control PHP Form Validation PHP Hypertext Pre Processor Common Serverside language powering 80 of websites ID: 782631
Download The PPT/PDF document "Web Programming Language" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Web Programming Language
Chapter 4
Slide2Introducing PHP
Setting Up a Web Server
PHP Basic Syntax
PHP Variables
PHP Operators
PHP Flow Control
PHP Form Validation
Slide3PHP
“Hypertext Pre Processor”
Common Server-side language powering 80% of websites
Core Part of WAMP stack (Windows Apache MySQL PHP)
Or LAMP, or MAMP
Slide4Set Up A Webserver
If you already have a webserver – great!
If not, set up your local machine as a host, by installing
wampserver
.
Create a file called hello.php
in your www folder
<?php echo “Hello World” ?>
Open it in a browser;
localhost/
hello.php
or 127.0.0.1/
hello.php
Slide5PHP Variables
Begin with $
Named by any combination of letters, numbers, and _
$Name
Case Sensitive
$NAME, $Name, $name are different variables
Can store different types of data
Strings, Integers, Floats, Boolean, Array & Objects
Weakly Typed
You don’t need to specify which type of data the variable will store
No need for declaration
Slide6PHP Variables
<?php
$x = 5;
$y = “3.5”;
$z = $x + $y;
echo "The number is $z<
br
>";
echo "The number is " . $z . "<
br
>";
?>
Opening PHP Tag
Concatenation
Closing PHP Tag
Assignment
Addition
Output
Slide7Arrays
Creates an Indexed array where “John” has index 0, and Fred has index 3.
<?php
$students = array(“John”, “Bob”, “Steve”);
$students[] = “Fred”;
echo “The first student is “ . $students[0];
?>
Slide8Associative Arrays
The Index could be a string
<?php
$ages = array(“John”=>”18”, “Bob”=>”19”, “Steve”=>”22”);
echo “John is “ . $ages[“John”];
?>
Each element is a “Key=>Value” Pair
Slide9Multidimensional Arrays
A 2 dimensional array is just an Array of Arrays
<?php
$students = array();
$students[] = array("John", "84", "A");
$students[] = array("Bob", "65", "C+");
$students[] = array("Steve", "72", "B");
echo "The first student's grade is " . $students[0][2];
?>
Slide10Strings
Strings are important in PHP (we are dealing with text on the web of course!)
Function Name
Purpose
explode()
Splits a string into an array
implode()
Converts an array into a string
lcfirst()
Makes the first character of a string lowercase
str_replace()
Replaces specified characters in a string
str_word_count
()
Returns the number of words in the string
strcmp()
Compares two strings
strlen()
Returns the length of a string
strpos()
Returns the position of the first occurrence of a string inside another string
strtoupper()
Converts a string to upper case
strtolower()
Converts a string to lower case
substr()
Returns a specified part of a string
ucfirst()
Makes the first character of a string uppercase
Slide11PHP Operators (Arithmetic)
Arithmetic Operators
Purpose
Example
+
Addition
$x=1+2;
-
Subtraction
$x=2-1;
*
Multiplication
$x=1*2;
/
Division
$x=2/1;
%
Modulus Returns the remainder from a division
$x=2%1;
++
Increment (add one to the value)
$x++;
--
Decrement (minus one from the value)
$x--;
Slide12PHP Operators (Assignment)
Assignment Operators
Example
Equivalent
=
$a = $b;
+=
$a += $b;
$a = $a + $b;
-=
$a -= $b;
$a = $a – $b;
*=
$a *= $b;
$a = $a * $b;
/=
$a /= $b;
$a = $a / $b;
%=
$a %= $b;
$a = $a % $b;
.=
$a .= $b
$a = $a . $b;
Slide13PHP Operators (Logical)
Logical Operators
Purpose
==
Equal to
!=
Not equal to
<>
Not equal to
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
&&
AND
||
OR
!
Not
Slide14PHP Flow Control (IF)
<?php
if($x>20)
{
echo “x is greater than 20”;
}
elseif($x<10)
{
echo “x is smaller than 20”;
}
else
{
echo “x is between 10 and 20”;
}?>
Slide15PHP Flow Control (Switch)
<?php
switch ($x) {
case “login”:
echo “Display the Login Page”;
break;
case “register”:
echo “Display the Register Page”;
break;
default:
echo “Display the Guest Page”;
break;
}
?>
Slide16PHP Flow Control (While)
<?php
$x = 10;
while($x>0)
{
echo “There were “ . $x . “ in the bed, till someone said, roll over…<
br
>”;
$x--;
}
?>
Slide17PHP Flow Control (Do…While)
<?php
$x = 10;
do {
echo “There were “ . $x . “ green apples, ready to be eaten…<
br
>”;
$x--;
} while($x>0);
?>
Slide18PHP Flow Control (For)
<?php
for($
i
= 0; $
i<10; $i
++)
{
echo “I have “ . $
i
. “ doors, but need 1 more…<
br
>”; }
?>
Slide19PHP Flow Control (Foreach)
<?php
$count = 1;
foreach($students as $name)
{
echo $count . “) Student name:- “ . $name . “<
br
>”;
$count++;
}
?>
Slide20PHP Flow Control (Foreach) Associative
<?php
$count = 1;
foreach($students as $name => $grade)
{
echo $count . “) Student “ . $name . “ got grade “ . $grade . “<
br
>”;
$count++;
}
?>
Slide21PHP Form Validation
HTML’s <form> tag is used to create a form that can send data to the server.
<form action="
welcome.php
" method="post">
Name: <input type="text" name="name"><
br
/>
<input type="submit" value="Go!">
</form>
Slide22$_POST
The previous form sends data by the POST method to
welcome.php
<body>
Welcome <?php echo $_POST[“name”]; ?>
</body>
Slide23$_GET
An alternative is to send the data via the GET method
<form action="
welcome.php
" method="get">
Name: <input type="text" name="name"><
br
/>
<input type="submit" value="Go!">
</form>
<body>
Welcome <?php echo $_GET[“name”]; ?>
</body>
What is the difference?
Slide24$_POST or $_GET
Both arrays are
Superglobals
, accessible from any function, class or file on the page.
Both create an array of key=>value pairs with the names of the form inputs and the values submitted
The GET method passes variables via URL parameters, while POST passes it via HTTP Post method.
The GET method is visible, so should not be used for sensitive data, but because it is visible, it can be bookmarked. However the URL is limited to around 2,000 characters.
Slide25print_r
()
A useful function for making a human readable version of an array
<body>
<?php
print_r
($_POST); ?>
</body>
Slide26Simple Form Validation
<body>
<?php
$name = $_POST["name"];
if(
strlen
($name)<3)
{
echo "Name must be at least 3 characters.";
}
?>
</body>
Slide27Key Points
PHP is an important Server Side programming language.
Before running PHP code a web server needs to be set up to process PHP code.
When a web server receives a request for PHP, first it processes the code before returning HTML.
Variables in PHP are weakly or loosely typed and can also be used to store arrays.
A form can be used to send data to the server using the post method.
Data sent via the post method then exists within the $_POST
superglobal
array, or using the get method to the $_GET
superglobal
array.
The main difference between post and get is that when using get, the parameters are attached to the
url
, so that it can be bookmarked.