Introduction to PHP
PHP (Recursive acronym for PHP: Hypertext
Preprocessor) is an open-source, interpreted, and
object-oriented scripting language that can be
executed at the server-side. PHP is well suited for web
development. Therefore, it is used to develop web
applications (an application that executes on the
server and generates the dynamic page.).
A PHP file end with the .php extension, example:
index.php
PHP and its common uses
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.
Developer can add, delete, and 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.
Characteristics of PHP
Five important characteristics make PHP's practical
nature possible:
Simplicity
Efficiency
Security
Flexibility
Familiarity
PHP Basic Syntax
Programming Syntax refers to the rules that define
the structure of a programming language.
Syntax in computer programming means the rules
that control the structure of the symbols, punctuation,
and words of a programming language. Without
syntax, the meaning or semantics of a language is
nearly impossible to understand. Therefore, the
structure which defines PHP computer language is
called PHP syntax.
Basic PHP syntax includes:
PHP Tags
Escaping from HTML
Instruction separation
Comments
Statement
Whitespace
PHP tags
When PHP parses a file, it looks for opening and
closing tags, which are <?php ?> which tell PHP to
start and stop interpreting the code between them.
Parsing in this manner allows PHP to be embedded in
all sorts of different documents that is capable of
accommodating it, as everything outside of a pair of
opening and closing tags is ignored by the PHP parser.
Example1:
<?PHP echo “welcome to PHP class”; ?>
This will print:
welcome to PHP class
Escaping from HTML
Everything outside of a pair of opening and closing tags is ignored by
the PHP parser which allows PHP files to have mixed content. This allows
PHP to be embedded in HTML documents, for example to create
templates.
Instruction separation
As in C or Perl, PHP requires instructions to be terminated with a
semicolon at the end of each statement.
PHP Comments
Comments are used for code documentation/information; it is
use to write any useful note alongside code, comments are
ignored by the parser. PHP has two built-in ways of commenting:
Single-line comments
Multiline comments
PHP Statement
Any PHP script is built out of a series of statements. A statement
is a code that does something, it can be an assignment of value
to variable, a function call, a loop, a conditional statement or
even a statement that does nothing (an empty statement).
Statements usually end with a semicolon.
PHP whitespace insensitivity
In general, whitespace is not visible on the screen, including spaces,
tabs, and end-of-line characters i.e. carriage returns. In PHP whitespace
doesn't matter in coding. You can break a single line statement to any
number of lines or number of separate statements together on a single
line.
PHP Embedded into HTML
PHP Comes up with its tags to demark PHP commands and
instructions within any of the aforementioned file documents i.e.
HTML, XML etc. To use PHP within HTML document, surround your
PHP code with <?php ?>. Everything between these markers is
interpreted as PHP, and everything outside the markers is not
PHP can be embedded within HTML document and the PHP
Parser will ignore the HTML code and only parse the PHP codes
that are within the PHP opening and closing tags as shown in the
in the previous examples.
Note: Refer to slide 7 and slide 8 to see example
PHP Variables and Constants
Variables and constant are used to store the data
Variables can be any value e.g. string, text, and numbers and it must
start with a $ symbol i.e. $name, $var_name, $age etc.
Variable values can change over the course of a script or program while
constants value cannot change.
Here’re some important things to know about variables:
In PHP, a variable need be declared before adding a value.
PHP automatically converts the variable to the correct data type,
depending on its value.
After declaring a variable it can be reused throughout the code. The
assignment operator (=) used to assign the value to a variable.
A variable is name given to memory location which is used to store
particular value. These values may be fixed or it can be dynamic.
Note: refer to slide 10 for example of a variable: $message = “hello”;
PHP Constants
Constant is a name or an identifier for a fixed value. Constant are like
variables except for when a constant is defined it cannot be undefined
or changed through out the program execution.
A valid constant name starts with a letter or underscore (no $ sign
before the constant name).
Constants are defined using PHP’s define() function, which accepts two
arguments: the name of the constant, and its value. Defined the
constant value the we can be accessed at any time just by referring to
its name.
PHP Super Global Variables
There are some predefined variable in PHP called the ‘superglobals’
which means that they are always accessible regardless of scope and
you can access them from any function, class or file without having to
do anything special.
PHP superglobals variables includes:
i. $GLOBALS
ii. $_SERVER
iii. $_REQUEST
iv. $_POST
v. $_GET
vi. $_ENV
vii. $_COOKIE
viii. $_SESSION
Note: We will take a look at and explore more on the super-global at the
appropriate session later in this course.
PHP Functions
A function is a block of code written in a program to perform some
specific task.
A function is a piece of code that can takes another input in the form of
parameter and process it, and then returns a value,
A PHP function can be reused over and over again.
PHP functions can be divided into two:
i. Built-in functions
ii. User defined functions
PHP Functions
Built-in Function User defined Function
These are functions that comes These are the functions that are
preinstalled with PHP. defined by the programmer as
per his/her specific desired
PHP provides these functions in
requirement.
the installation package itself
which makes this language more User defined functions are
powerful and useful. To use the created by the programmer to
properties of the function we just suites what he want to achieve.
need to call the function
wherever required to fetch the
desired result.
Built-in Function
PHP provides us with huge collection of in-built library functions. These
functions are already coded and stored in form of functions. To use
them, we identify what we want to do and call them appropriately.
User Defined Function
PHP allows us to create our own customized functions called the User
Defined Functions.
Function Parameter vs. Argument
Function Parameters: these are the names listed in the definition of a
function.
Function Arguments: these are the real values that are passed when
calling a function
The end