PHP
PHP
WebServer
Server Side Scripts
• Run on a server, embedded in the site’s code (HTML
mostly)
• Designed to interact with backend infrastructure -
Database, Communications and other services
• Facilitates transfer of data from server to browser
(with some filtering, formatting etc)
• Runs on a call
• Powers the dynamic execution of web applications
• Plays a role in how database is designed
• Used to build Application programming Interfaces
XAMPP
• XAMPP is the most popular PHP development
environment
• XAMPP is a completely free, easy to install
Apache distribution containing MariaDB, PHP,
and Perl. The XAMPP open source package has
been set up to be incredibly easy to install and
to use.
Cross(X)platform Apache MariaDB PHP Perl
• Download from
https://www.apachefriends.org/index.html
• <!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
Variables
• <?php
echo strlen("Hello world!"); // outputs
12
?>
• <?php
echo str_word_count("Hello world!"); //
outputs 2
?>
• <?php
echo strrev("Hello world!"); // outputs
!dlrow olleH
?>
• <?php
echo strpos("Hello
world!", "world"); // outputs 6
?>
• <?php
echo str_replace("world", "Dolly",
"Hello world!"); // outputs Hello
Dolly!
?>
PHP Math
• <?php
echo(pi()); //returns3.1415926535
?>
• <?php
echo(min(0, 150, 30, 20, -8, -200)); //
returns -200
echo(max(0, 150, 30, 20, -8, -200)); //
returns 150
?>
• <?php
echo(abs(-6.7)); // returns 6.7
?>
• <?php
echo(sqrt(64)); // returns 8
?>
• <?php
echo(round(0.60)); // returns 1
echo(round(0.49)); // returns 0
?>
• <?php
echo(rand());
?>
• <?php
echo(rand(10, 100));
?>
• PHP divides the operators in the following
groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
Output "Have a good day!" if the current time (HOUR) is less
than 20, and "Have a good night!" otherwise:
• <?php
$t = date("H");
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
• <?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
PHP Array
• <?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
• <?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"4
3");
echo "Peter is " . $age['Peter'] . " years
old.";
?>
PHP Form Handling
• <!DOCTYPE HTML>
<html>
<body>
</body>
</html>
PHP DATE
• Here are some characters that are commonly used for
dates:
• d - Represents the day of the month (01 to 31)
• m - Represents a month (01 to 12)
• Y - Represents a year (in four digits)
• l (lowercase 'L') - Represents the day of the week
• <?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
INCLUDE
• <html>
<body>
</body>
</html>
• <?php
echo "<p>Copyright © 1999-" . date("Y")
. " W3Schools.com</p>";
?>
PHP Cookie
• What is a Cookie?
• A cookie is often used to identify a user.
• A cookie is a small file that the server
embeds on the user's computer.
• Each time the same computer requests a
page with a browser, it will send the
cookie too.
• With PHP, you can both create and retrieve
cookie values.
PHP Create/Retrieve a Cookie
The following example creates a cookie named "user" with the value "John ". The cookie will expire
after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise,
select the directory you prefer).We then retrieve the value of the cookie "user" (using the global
variable $_COOKIE). We also use the isset() function to find out if the cookie is set:
• <?php
$cookie_name = "user";
$cookie_value = "John";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?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>