Client Side & Server Side Scripting and Introduction To PHP
Client Side & Server Side Scripting and Introduction To PHP
Introduction to PHP
Server side scripting
The PHP code is enclosed in special start and end processing instructions <?
php and ?> that allow you to jump into and out of "PHP mode."
PHP Introduction
PHP Introduction
PHP code is executed on the server, generating HTML which is then sent to
the client.
The client would receive the results of running that script, but would not know
what the underlying code was.
PHP Introduction
PHP Getting Started
On windows, you can download and install XAMP. With one installation and
you get an Apache webserver, database server and php.
A variable starts with the $ sign, followed by the name of the variable
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)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = "John";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
echo " $txt!";
?>
</body>
</html>
Type of the variable
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
var_dump($x);
?>
</body>
</html>
OUTPUT: int(5)
Assigning multiple values
<!DOCTYPE html>
<html>
<body>
<?php
$x = $y = $z = "Fruit";
echo $x;
echo $y;
echo $z;
?>
</body>
</html>
Conditional statements:
If else
<?php
if (5 > 3) {
echo "Have a good day!";
}
?>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
PHP Loops
<?php
$i = 1;
OUPTUT: 12345
<!DOCTYPE html>
<html>
<body>
<?php
$i = 1;
</body>
</html>
do while
<!DOCTYPE html>
<html>
<body>
<?php
$i = 1;
do {
echo $i;
$i++;
} while ($i < 6);
?>
</body>
</html>
OUTPUT: 12345
For loop
for (expression1, expression2, expression3) {
// code block
}
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
foreach loop
<!DOCTYPE html>
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
</body>
</html>
Php functions
<!DOCTYPE html>
<html>
<body>
<?php
function myMessage() {
echo "Hello world!";
}
myMessage();
?>
</body>
</html>
OUTPUT: Hello world!
Function arguments
<!DOCTYPE html>
<html>
<body>
<?php
function familyName($fname) {
echo "$fname joy.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Alex");
?>
</body>
</html>
Arrays
An array is a special variable that can hold many values under a single
name, and you can access the values by referring to an index number or
name.
In PHP, there are three types of arrays:
Output: array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota“}
Php form handling
The PHP superglobals $_GET and $_POST are used to collect
form-data.
<html>
<body>
Welcome
<?php echo $_POST["name"]; ?><br> // echo $_GET["name"];
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
GET vs POST
Both GET and POST create an array (e.g. array( key1 => value1,
key2 => value2, key3 => value3, ...)).
This array holds key/value pairs, where keys are the names of the
form controls and values are the input data from the user.
$_GET is an array of variables passed to the current script via
the URL parameters.