2. PHP-Introduction
• PHP is a server scripting language, and a
powerful tool for making dynamic and
interactive Web pages.
• PHP is a widely-used, free, and efficient
alternative to competitors such as
Microsoft's ASP.
• PHP 7 is the latest stable release.
3. Why to learn PHP?
• Some of the key advantages of learning PHP:
PHP is a recursive acronym for "PHP: Hypertext
Preprocessor".
PHP is a server side scripting language that is embedded in
HTML. It is used to manage dynamic content, databases,
session tracking, even build entire e-commerce sites.
It is integrated with a number of popular databases, including
MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft
SQL Server.
4. PHP supports a large number of major protocols such as
POP3, IMAP, and LDAP. PHP4 added support for Java and
distributed object architectures (COM and CORBA), making n-
tier development a possibility for the first time.
PHP Syntax is C-Like.
5. Characteristics of PHP
• Five important characteristics make PHP's practical
nature possible
1. Simplicity
2. Efficiency
3. Security
4. Flexibility
5. Familiarity
6. Applications of PHP
• PHP is one of the most widely used language over the web.
PHP performs system functions, i.e. from files on a system it can
create, open, read, write, and close them.
PHP can handle forms, i.e. gather data from files, save data to a
file, through email you can send data, return data to the user.
You add, delete, modify elements within your database through
PHP.
Access cookies variables and set cookies.
Using PHP, you can restrict users to access some pages of your
website.
It can encrypt data.
7. PHP - Environment
• In order to develop and run PHP Web pages three vital components
need to be installed on your computer system.
Web Server − PHP will work with virtually all Web Server software,
including Microsoft's Internet Information Server (IIS) but then most
often used is freely available Apache Server. Download Apache for
free here − https://httpd.apache.org/download.cgi
Database − PHP will work with virtually all database software,
including Oracle and Sybase but most commonly used is freely
available MySQL database. Download MySQL for free here
− https://www.mysql.com/downloads/
PHP Parser − In order to process PHP script instructions a parser
must be installed to generate HTML output that can be sent to the
Web Browser.
8. History of PHP
PHP was originally created by a developer named Rasmus Lerdorf.
10. • Error Reporting
• Loosely Typed Language
• Web servers Support
• Security
• Control
11. PHP Installation
• To start using PHP,
• Find a web host with PHP and MySQL support
• Install a web server on your own PC, and then install
PHP and MySQL
12. Set Up PHP on Your Own PC
• However, if your server does not support PHP, you
must:
1. install a web server
2. install PHP
3. install a database, such as MySQL
13. PHP Software Requirement
PHP Server
• The PHP Community Provides Some types of Software
Server solution under The GNU (General Public
License).
• These are the following:
1.WAMP Server
2.LAMP Server
3.MAMP Server
4.XAMPP Server
14.
15. • WAMP for Windows
• LAMP for Linux
• MAMP for Mac
• SAMP for Solaris
• FAMP for FreeBSD
• XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross
Platform: It includes some other components too such
as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.
16. 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”
A PHP file normally contains HTML tags, and some PHP
scripting code
19. The PHP command ‘echo’ is used to output
the parameters passed to it
◦ The typical usage for this is to send data to the
client’s web-browser
Syntax
◦ void echo (string arg1 [, string argn...])
◦ In practice, arguments are not passed in
parentheses since echo is a language construct
rather than an actual function
Echo
20. • PHP echo statement can be used to print the string,
multi-line strings, escaping characters, variable, array,
etc.
• Some important points about the echo statement are:
• echo is a statement, which is used to display the output.
• echo can be used with or without parentheses: echo(), and
echo.
• echo does not return any value.
• We can pass multiple strings separated by a comma (,) in
echo.
• echo is faster than the print statement.
21. PHP echo: printing string
<?php
echo "Hello by PHP echo";
?>
Output:
Hello by PHP echo
22. PHP echo: printing multi line string
<?php
echo "Hello by PHP echo
this is multi line
text printed by
PHP echo statement
";
?>
Output:
Hello by PHP echo this is multi line text printed by PHP echo
statement
24. PHP echo: printing variable value
<?php
$msg="Hello JavaTpoint PHP";
echo "Message is: $msg";
?>
Output:
Message is: Hello JavaTpoint PHP
25. PHP echo and print statements
• More or less same. Both are used to display data.
But differences are small:
1. echo has no return value while
print has a return value of 1 so it can be used in
expressions.
2. echo can take multiple parameters while
print can take one argument.
3. echo is marginally faster than print
26. The PHP print Statement
The print statement can be used with or
without parentheses: print or print().
Display Text
The following example shows how to output
text with the print command (Notice that the
text can contain HTML markup):
28. Display Variables
The following example shows how to output text and
variables with the print statement:
Example
<?php
$txt1 = “Hello";
$txt2 = “PHP";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print “Welcome to " . $txt2 . "<br>";
print $x + $y;
?>
29. PHP Case Sensitivity
• In PHP, keywords (e.g. if, else, while, echo, etc.), classes,
functions, and user-defined functions are not case-sensitive.
• In the example below, all three echo statements below are
equal and legal.
• <!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
• Note: All three echo statements are equal and valid
30. Example for case sensitive
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
• Note: Here $color, $COLOR, and $coLOR are treated as three
different variables . First statement is true.
31. PHP Comments
• A comment in PHP code is a line that is not executed as
a part of the program. Its only purpose is to be read by
someone who is looking at the code.
• Comments can be used to:
Let others understand our code
Remind yourself of what you did - Most programmers
have experienced coming back to their own work a
year or two years later and having to re-figure out
what they did. Comments can remind you of what you
were thinking when you wrote the code.
32. PHP supports several ways of commenting:
• Syntax for single-line comments
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
?>
</body>
</html>
33. • Syntax for multiple-line comments:
<!DOCTYPE html>
<html>
<body>
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
</body> </html>
34. PHP Variables
• Variables are "containers" for storing
information.
Creating (Declaring) PHP Variables
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
35. PHP Variables
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
Rules for PHP variables:
•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)
•Note: Remember that PHP variable names are case-
sensitive!
36. Output Variables
• The PHP echo statement is often used to
output data to the screen.
• The following example will show how to output
text and a variable:
Example 1
<?php
$txt = “PHP";
echo "I like $txt!";
?>
37. Example 2
<?php
$txt = “PHP";
echo "I like " . $txt . "!";
?>
Note: The above example will produce the same output
as the previous one.
39. PHP Variables Scope
• In PHP, variables can be declared anywhere in the script
• The scope of a variable is the part of the script where
the variable can be referenced/used.
• PHP has three different variable scopes:
1. local
2. global
3. static
40. Global and Local Scope
• A variable declared outside a function has a GLOBAL
SCOPE and can only be accessed outside a function
Example - Variable with global scope
<?php
$x = 5; // global scope
function display() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
display();
echo "<p>Variable x outside function is: $x</p>";
?>
41. • A variable declared within a function has a LOCAL SCOPE
and can only be accessed within that function
Example
Variable with local scope:
<?php
function display() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
display();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
42. PHP The static Keyword
• Normally, when a function is completed/executed, all of its
variables are deleted. However, sometimes we want a local
variable NOT to be deleted. We need it for a further job.
• So declare the variable as static
<?php
function display()
{
static $x = 0;
echo $x;
$x++;
}
display();
display();
display();
?>
43. The global keyword is used to access a global variable
from within a function.
To do this, use the global keyword before the variables
(inside the function):
PHP The global Keyword
44. Example
<?php
$x = 5;
$y = 10;
function add() {
global $x, $y;
$y = $x + $y;
}
add();
echo $y; // outputs 15
?>
45. GLOBALS
• PHP also stores all global variables in an array
called $GLOBALS[index].
• The index holds the name of the variable. This array is
also accessible from within functions and can be used to
update global variables directly.
The example above can be rewritten like this:
47. PHP Data Types
• Variables can store data of different types, and different
data types can do different things.
PHP supports the following data types:
1. String
2. Integer
3. Float (floating point numbers - also called double)
4. Boolean
5. Array
6. Object
7. NULL
8. Resource
48. PHP Numbers
PHP provides automatic data type conversion.
So, if you assign an integer value to a variable, the type
of that variable will automatically be an integer. Then, if
you assign a string to the same variable, the type will
change to a string.
Example:
$x=12;
$x=‘Hello’;
49. PHP Integers
• An integer is a number without any decimal part.
• Example: 2, 256, -256, 10358, -179567 are all integers.
• So, an integer data type is a non-decimal number between -2147483648 and
2147483647.
• Here are some rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in three formats: decimal (10-based), hexadecimal
(16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
50. • PHP has the following functions to check if the type of a variable
is integer:
is_int()
is_integer() - alias of is_int()
is_long() - alias of is_int()
Check if the type of a variable is integer:
<?php
$x = 5985;
var_dump(is_int($x));
$x = 59.85;
var_dump(is_int($x));
?>
51. PHP Float
• A float is a number with a decimal point or a number in
exponential form.
• 2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats.
• The float data type can commonly store a value up to
1.7976931348623E+308 (platform dependent), and
have a maximum precision of 14 digits.
PHP has the following functions to check if the type of a
variable is float:
is_float()
is_double() - alias of is_float()
52. Example
Check if the type of a variable is float:
<?php
$x = 10.365;
var_dump(is_float($x));
?>
53. PHP Infinity
• A numeric value that is larger than PHP_FLOAT_MAX is considered infinite.
• PHP has the following functions to check if a numeric value is finite or infinite:
is_finite()
is_infinite()
• However, the PHP var_dump() function returns the data type and
value:
Example
Check if a numeric value is finite or infinite:
<?php
$x = 1.9e411;
var_dump($x);
?>
54. PHP-NaN
• NaN stands for Not a Number.
• NaN is used for impossible mathematical operations.
• PHP has the following functions to check if a value is not a number:
is_nan()
• Here, the PHP var_dump() function returns the data type and value:
Example
• Invalid calculation will return a NaN value:
<?php
$x = acos(8);
var_dump($x);
?>
55. PHP String
• A string is a sequence of characters, like "Hello world!".
• A string can be any text inside quotes. We can use single or double
quotes:
Example
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
56. PHP Integer
• An integer data type is a non-decimal number between
-2,147,483,648 and 2,147,483,647.
• Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in: decimal (base 10),
hexadecimal (base 16), octal (base 8), or binary (base
2) notation
58. PHP Float
• A float (floating point number) is a number with a
decimal point or a number in exponential form.
• Here $x is a float. The PHP var_dump() function returns
the data type and value:
Example
<?php
$x = 10.365;
var_dump($x);
?>
59. PHP Boolean
• A Boolean represents two possible states:
TRUE or FALSE.
$x = true;
$y = false;
• Booleans are often used in conditional
testing
60. PHP Array
• An array stores multiple values in one single variable.
• In the following example $days is an array. The PHP
var_dump() function returns the data type and value:
Example
<?php
$days = array(“Sunday",“Monday”,"Tuesday");
var_dump($days);
?>
61. PHP Object
• An object is a data type which stores data and
information on how to process that data.
• In PHP, an object must be explicitly declared.
• To declare a class of object, the keyword class is used.
A class is a structure that can contain properties and
methods:
62. PHP NULL Value
• Null is a special data type which can have only one
value: NULL.
• A variable of data type NULL is a variable that has no
value assigned to it.
• Tip: If a variable is created without a value, it is
automatically assigned a value of NULL.
• Variables can also be emptied by setting the value to
NULL:
64. PHP Resource
• The special resource type is not an actual data type. It
is the storing of a reference to functions and resources
external to PHP.
• A common example of using the resource data type is a
database call.
65. Summary
• PHP was created by Rasmus Lerdorf in
1994 but appeared in the market in 1995. PHP
7.4.0 is the latest version of PHP, which was
released on 28 November.
• Some important points need to be noticed about
PHP are as followed.
• PHP stands for Hypertext Preprocessor.
• PHP is an interpreted language, i.e., there is no need
for compilation.
66. • PHP is faster than other scripting languages, for
example, ASP and JSP.
• PHP is a server-side scripting language, which is
used to manage the dynamic content of the
website.
• PHP can be embedded into HTML.
• PHP is an object-oriented language.
• PHP is an open-source scripting language.
• PHP is simple and easy to learn language.