PHP(Hypertext Preprocessor)
• PHP is a server scripting language, and a
powerful tool for making dynamic and
interactive Web pages.
• PHP is a widely-used, free
• PHP scripts are executed on the server
• PHP is free to download and use
• What is a PHP File?
• PHP files can contain text, HTML, CSS,
JavaScript, and PHP code
• PHP code is executed on the server, and the
result is returned to the browser as plain
HTML
• PHP files have extension ".php"
• What Can PHP Do?
• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and
close files on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your
database
• PHP can be used to control user-access
• PHP can encrypt data
• Why PHP?
• PHP runs on various platforms (Windows, Linux,
Unix, Mac OS X, etc.)
• PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
• PHP supports a wide range of databases
• PHP is free. Download it from the official PHP
resource: www.php.net
• PHP is easy to learn and runs efficiently on the
server side
• Set Up PHP on Your Own PC
• However, if your server does not support PHP,
you must:
• install a web server
• install PHP
• install a database, such as MySQL
• The official PHP website (PHP.net) has installation
instructions for
PHP: http://php.net/manual/en/install.php
PHP Syntax
• A PHP script is executed on the server, and the
plain HTML result is sent back to the browser.
• Basic PHP Syntax
• A PHP script can be placed anywhere in the
document.
• A PHP script starts with <?php and ends with ?>:
• <?php
// PHP code goes here
?>
• The default file extension for PHP files is ".php".
• <!DOCTYPE html>
• <html>
• <body>
• <h1>My first PHP page</h1>
• <?php
• echo "Hello World!";
• ?>
• </body>
• </html>
PHP comments
• <!DOCTYPE html>
• <html>
• <body>
• <?php
• // This is a single-line comment
• # This is also a single-line comment
• ?>
• </body>
• </html>
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
Creating (Declaring) PHP Variables
• In PHP, a variable starts with the $ sign, followed
by the name of the variable:
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
PHP echo and print Statements
• With PHP, there are two basic ways to get
output: echo and print.
• echo and print are more or less the same.
They are both used to output data to the
screen.
PHP Data Types
• Variables can store data of different types, and
different data types can do different things.
• PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP Array
• An array stores multiple values in one single
variable.
• In the following example $cars is an array. The
PHP var_dump() function returns the data type
and value:
• Example
• <?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Object
• Classes and objects are the two main aspects of
object-oriented programming.
• A class is a template for objects, and an object is
an instance of a class.
• When the individual objects are created, they
inherit all the properties and behaviors from the
class, but each object will have different values
for the properties.
• Let's assume we have a class named Car. A Car
can have properties like model, color, etc. We can
define variables like $model, $color, and so on, to
hold the values of these properties.
• When the individual objects (Volvo, BMW, Toyota,
etc.) are created, they inherit all the properties
and behaviors from the class, but each object will
have different values for the properties.
• If you create a __construct() function, PHP will
automatically call this function when you create
an object from a class.
1.php
PHP Strings
• A string is a sequence of characters, like "Hello world!".
• PHP String Functions
• strlen() - Return the Length of a String
• str_word_count() - Count Words in a String
• strrev() - Reverse a String
• strpos() - Search For a Text Within a String
• explode() - Breaks a string into an array
• strtolower() - Converts a string to lowercase letters
• strtoupper() - Converts a string to uppercase letters
• trim() - Removes whitespace or other characters from
both sides of a string