PHP Introduction
•PHP code is executed on the server.
•PHP is an acronym for "PHP: Hypertext
Preprocessor"
•PHP is a widely-used, open source
scripting language
•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
PHP
•Create a file named hello.phpand save it in the root directory of your web
server with the following content.
•Now, enter your web server's URL in your browser, ending with /hello.php. As
you are using a web server installed on PC, the full URL might be
http://localhost/hello.php orhttp://127.0.0.1/hello.php.
PHP Tags
•Opening Tag
•Closing Tag
•When you request a PHP file, the server process the file using the PHP parser.
The parsed output will be sent as the response.
•When PHP parses a file, it searches for opening and closing tags.
•All the code inside these tags is interpreted. Everything outside these tags is
ignored by the PHP parser.
PHP Comments
•Single line comment
•Multiline comments
PHP Variables
•Variables are used to store information and retrieve them when needed.
•They can be labeled with a meaningful name to be understood easily by the
readers and ourselves.
❑The $ identifier should be added in front of the name of the variable.
❑A variable name must start with a letter or underscore.
❑A variable name cannot start with a number.
❑A variable name cannot be $this. ($this is a special variable which cannot be assigned)
❑A variable name can contain letters, numbers, and characters between 127-255 ASCII
after the first character.
❑Variable names are case sensitive.
PHP Variables
PHP Variables
•In programming, creating a variable is called declaring.
•But, in PHP, unlike other programming languages, there is no keyword to
declare a variable.
PHP Variables
❑Strings should be wrapped with " (double-quotes) or '
(single-quotes)..
❑The variable name must be on the left.
❑The= sign as the assignment operator.
❑Next, the value to be assigned.
❑Finally,; sign to say the statement is finished.
PHP Variables
•Strings should be wrapped with " (double-quotes) or '
(single-quotes)..
❑The variable name must be on the left.
❑The= sign as the assignment operator.
❑Next, the value to be assigned.
❑Finally,; sign to say the statement is finished.
PHP Variables
•Example: Output is “World”
PHP Variable Scope
Global Scope
•A variable declared in the main flow of the code (not inside a function) has
a global scope. These variables can't be used inside functions.
PHP Variable Scope
Local Scope
•A variable declared inside a function has a local scope.
•This variable is unique to this function and can only be accessed within the
function.
•You can define different variables with the same name in different
functions.
PHP Variable Scope
Global Variables in Local Scope
•You can use global keyword inside a function to access global
variables.
•Before using variables, add global keyword followed by comma-
separated variable names you need to use inside the function.
PHP Variable Scope
Global Variables in Local Scope
•Local scope variables are deleted after the end of the execution
of the function. But, sometimes we need to keep the variable alive.
•To use this feature, add static keyword before the variable when
declaring it.
PHP Constants
•PHP does not have a keyword to declare a constant. We have to use
define() function for the purpose
❑Once a constant is defined, it cannot be changed.
❑All the constants have global scopes, even it is defined inside a function.
❑Unlike variables, constants do not have $ before the name.
❑A constant name should start with a letter or underscore.
❑A constant name can have letters, numbers, ASCII characters between 127-255 after
the first letter..
PHP Constants
Constants
PHP Strings
•The easiest way to declare a string is by enclosing it by single quotes.
Character:‘
•What if you needed to add a single quote inside a single quoted string?
Just escape that character with a back slash. \\\\\\\\\\\\\\\
PHP Strings
Strings
•What if you needed to have \ in a single quoted string?
•Use \\to escape the \character.
PHP Strings
Double Quoted Strings
•Strings can be declared enclosed by double quotes. Character: “”
•In single quotes, only \\\\\\and \\' had a special meaning. But, in double
quotes, there are more escape sequences.
PHP Output
PHP Output
•In PHP, there are two output statements. echo, print
PHP Data types
PHP Data types
There are several data types in PHP.
•Boolean
•Integer
•Float or Double
•String
•Array
•Object
•Null
PHP Data types
var_dump
❑var_dump function dumps information about a variable.
❑The importance of this function is, it outputs the data type of the variable
with its value. It is also aoutput function like echo()
PHP Data types
Phparray
There are two ways to declare an array.
•An array can be declared using the array() function.
•An array can be declared wrapping with [ and ].
•Elements of the arrays should be separated with a comma. And,elements
can be any type
PHP Data types
Phpnull
•Nullmeans no value.
•Nullis not 0, false or any other value, it is null.
PHP Operators
Arithmetic Operators
PHP Operators
Assignment Operators
•The basic assignment operator in PHP is =.It is actually not the "equal" mark in PHP.
•It works as assignment operator, which assigns a value to a variable.
In this example,
•5 is assigned to $a.
•7 is assigned to $b.
•Then, the value of $b (7) is assigned to$a.
PHP Operators
Assignment Operators
PHP Operators
Comparison Operators
PHP Operators
Logical Operators
PHP Operators
String Operators
PHP Conditionals
Conditionals
In PHP, we have the following conditional statements:
•If Statement
•If-Else Statement
•If-Elseif-Else Statement
•Switch Statement
PHP Conditionals
If Statement
PHP Conditionals
If else
PHP Conditionals
If-Elseif-Else Statement
PHP Conditionals
Switch
PHP Loops
•Often when you write code, you want the same
block of code to run over and over again in a row.
•Instead of adding several almost equal lines in a
script, we can use loops to perform a task like this.
•In PHP, we have the following looping statements:
–while-loops through a block of code while a specified
condition is true
–do...while -loops through a block of code once, and
then repeats the loop as long as a specified condition
is true
–for-loops through a block of code a specified number
of times
–foreach -loops through a block of code for each
element in an array
PHP Functions
a. User defined Functions
PHP Functions
User defined Functions
Function Argument
The PHP Date()
•The PHP date() function is used to format a date
and/or a time.
•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 Forms -$_GET
Function
•The PHP superglobals$_GET and $_POST
are used to collect form-data.
•The built-in $_GET function is used to
collect values from a form sent with
method="get".
–Information sent from a form with the GET
method is visible to everyone (it will be
displayed in the browser’s address bar) and
has limits on the amount of information to
send (max. 100 characters).
PHP Forms -$_GET
Function
•When using method="get" in HTML forms, all
variable names and values are displayed in
the URL.
•This method should not be used when
sending passwords or other sensitive
information!
•However, because the variables are displayed
in the URL, it is possible to bookmark the
page. This can be useful in some cases.
•The get method is not suitable for large
variable values;
PHP Forms -$_POST
Function
•The built-in $_POST function is used to
collect values from a form sent with
method="post".
•Information sent from a form with the
POST method is invisible to others and has
no limits on the amount of information to
send.
PHP Forms -$_POST
Function
When to use method="post"?
•Information sent from a form with the
POST method is invisible to others and has
no limits on the amount of information to
send.
•However, because the variables are not
displayed in the URL, it is not possible to
bookmark the page.