0% found this document useful (0 votes)
47 views26 pages

Unit 1 - PHP

Php notes

Uploaded by

jumansobarina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views26 pages

Unit 1 - PHP

Php notes

Uploaded by

jumansobarina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to PHP

PHP started out as a small open source project that evolved as more and more people
found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way
back
in 1994.

 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 ecommerce
sites.

 It is integrated with a number of popular databases, including MySQL, PostgreSQL,


Oracle, Sybase, Informix, and Microsoft SQL Server.

 PHP is pleasingly zippy in its execution, especially when compiled as an Apache


module on the Unix side. The MySQL server, once started, executes even very
complex queries with huge result sets in record-setting time.

 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 is forgiving: PHP language tries to be as forgiving as possible.


 PHP Syntax is C-Like.

Common Uses of PHP

PHP performs system functions, i.e. from files on a system it can create, open, read,
write,
and close them. The other uses of PHP are:
 PHP can handle forms, i.e. gather data from files, save data to a file, thru email
you can send data, return data to the user.

 You add, delete, modify elements within your database thru PHP.

 Access cookies variables and set cookies.


 Using PHP, you can restrict users to access some pages of your website.

Page | 1
Characteristics of PHP
Five important characteristics make PHP's practical nature possible:

 Simplicity

 Efficiency

 Security

 Flexibility
 Familiarity

What is PHP?
 PHP (PHP: Hypertext Preprocessor) is a widely-used open source general-
purpose scripting language that is especially suited for web development and can
be embedded into HTML.

 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

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".

<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

Page | 2
</body>
</html>

PHP Case Sensitivity

In PHP, all 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 legal (and equal):

Example

<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body>
</html>

Output:

Hello World!
Hello World!
Hello World!

Creating (Declaring) PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable:

Example

<?php
$txt = "Hello world!";

Page | 3
$x = 5;
$y = 10.5;
?>

Run example »

After the execution of the statements above, the variable $txt will hold the value Hello
world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.

All variable names are case-sensitive.

In the example below, only the first statement will display the value of the $color
variable (this is because $color, $COLOR, and $coLOR are treated as three different
variables):

Example

<!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>
Output:

My car is red
My house is
My boat is

Page | 4
Comments in PHP

A comment in PHP code is a line that is not read/executed as 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 what you are doing


 Remind yourself of what you did - Most programmers have experienced coming
back to their own work a year or two later and having to re-figure out what they
did. Comments can remind you of what you were thinking when you wrote the
code

PHP supports several ways of commenting:

<html>
<body>

<?php
// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/

$x = 5 /* + 15 */ + 5;
echo $x;
?>

</body>
</html>

Output

10

Page | 5
Statements are expressions terminated by semicolons

A statement in PHP is any expression that is followed by a semicolon (;).Any sequence


of
valid PHP statements that is enclosed by the PHP tags is a valid PHP program. Here is
a
typical statement in PHP, which in this case assigns a string of characters to a variable
called $greeting:

$greeting = "Welcome to PHP!";

Expressions are combinations of tokens


The smallest building blocks of PHP are the indivisible tokens, such as numbers
(3.14159),
strings (.two.), variables ($two), constants (TRUE), and the special words that make up
the syntax of PHP itself like if, else, while, for and etc.,

PHP ─ Variable Types

The main way to store information in the middle of a PHP program is by using a
variable.
Here are the most important things to know about variables in PHP.
All variables in PHP are denoted with a leading dollar sign ($).
The value of a variable is the value of its most recent assignment.
Variables are assigned with the = operator, with the variable on the left-hand side
and the expression to be evaluated on the right.
Variables can, but do not need, to be declared before assignment.

PHP has a total of eight data types which we use to construct our variables:
Integers: are whole numbers, without a decimal point, like 4195.
Doubles: are floating-point numbers, like 3.14159 or 49.1.

Page | 6
Booleans: have only two possible values either true or false.
NULL: is a special type that only has one value: NULL.
Strings: are sequences of characters, like 'PHP supports string operations.'
Arrays: are named and indexed collections of other values.
Objects: are instances of programmer-defined classes, which can package up both
other kinds of values and functions that are specific to the class.
Resources: are special variables that hold references to resources external to PHP
(such as database connections).

Integers
They are whole numbers, without a decimal point, like 4195. They are the simplest
type .they correspond to simple whole numbers, both positive and negative. Integers
can
be assigned to variables, or they can be used in expressions, like so:
$int_var = 12345;
$another_int = -12345 + 12345;

Doubles
They like 3.14159 or 49.1. By default, doubles print with the minimum number of
decimal
places needed. For example, the code:
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $many + $many_2;

Boolean
They have only two possible values either true or false. PHP provides a couple of
constants
especially for use as Booleans: TRUE and FALSE, which can be used like so:
if (TRUE)
print("This will always print<br>");

Page | 7
else
print("This will never print<br>");

NULL
NULL is a special type that only has one value: NULL. To give a variable the NULL
value,
simply assign it like this:
$my_var = NULL;

Strings
They are sequences of characters, like "PHP supports string operations". Following are
valid examples of string:
$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, singly quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters

Variable Naming
Rules for naming a variable is:
Variable names must begin with a letter or underscore character.
A variable name can consist of numbers, letters, underscores but you cannot use
characters like + , - , % , ( , ) . & , etc
There is no size limit for variables.

PHP – Variables
Scope can be defined as the range of availability a variable has to the program in which
it
is declared. PHP variables can be one of four scope types:
Local variables

Page | 8
Function parameters
Global variables
Static variables

PHP Local Variables


A variable declared in a function is considered local; that is, it can be referenced solely
in that function.

PHP Function Parameters


PHP Functions are covered in detail in PHP Function Chapter. In short, a function is a
small unit of program which can take some input in the form of parameters and does
some processing and may return a value.

PHP Global Variables


In contrast to local variables, a global variable can be accessed in any part of the
program. However, in order to be modified, a global variable must be explicitly declared
to be global in the function in which it is to be modified. This is accomplished,
conveniently enough, by placing the keyword GLOBAL in front of the variable that
should be recognized as global.

PHP Static Variables


The final type of variable scoping that I discuss is known as static. In contrast to the
variables declared as function parameters, which are destroyed on the function's exit, a
static variable will not lose its value when the function exits and will still hold that value
should the function be called again.

Page | 9
PHP ─ Operator Types

What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9.


Here 4 and 5 are called operands and + is called operator. PHP language supports
following type of operators.

Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators

Arithmetic Operators
The following arithmetic operators are supported by PHP language:
Assume variable A holds 10 and variable B holds 20 then:

Example
Try the following example to understand all the arithmetic operators. Copy and paste
following PHP program in test.php file and keep it in your PHP Server's document root
and
browse it using any browser.

<html>
<head><title>Arithmetical Operators</title><head>

Page | 10
<body>
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addition Operation Result: $c <br/>";
$c = $a - $b;
echo "Subtraction Operation Result: $c <br/>";
$c = $a * $b;
echo "Multiplication Operation Result: $c <br/>";
$c = $a / $b;
echo "Division Operation Result: $c <br/>";
$c = $a % $b;
echo "Modulus Operation Result: $c <br/>";
$c = $a++;
echo "Increment Operation Result: $c <br/>";
$c = $a--;
echo "Decrement Operation Result: $c <br/>";
?>
</body>
</html>

This will produce the following result:


Addition Operation Result: 62
Subtraction Operation Result: 22
Multiplication Operation Result: 840
Division Operation Result: 2.1
Modulus Operation Result: 2
Increment Operation Result: 42
Decrement Operation Result: 43

Page | 11
Comparison Operators
There are following comparison operators supported by PHP language.
Assume variable A holds 10 and variable B holds 20 then:

Logical Operators
The following logical operators are supported by PHP language.
Assume variable A holds 10 and variable B holds 20 then:

Page | 12
Assignment Operators
PHP supports the following assignment operators:

Conditional Operator

There is one more operator called the conditional operator. It first evaluates an
expression
for a true or false value and then executes one of the two given statements depending
upon the result of the evaluation.

Page | 13
Precedence of PHP Operators
Operator precedence determines the grouping of terms in an expression. This affects
how an expression is evaluated. Certain operators have higher precedence than others;
for example, the multiplication operator has higher precedence than the addition
operator: For example, x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator *
has higher precedence than + so it first get multiplied with 3*2 and then adds into 7.
Here operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom. Within an expression, higher precedence operators will
be evaluated first.

Page | 14
PHP ─ Decision Making
The if, elseif ...else and switch statements are used to take decision based on the
different condition. You can use conditional statements in your code to make your
decisions. PHP supports the following three decision making statements:

if...else statement - use this statement if you want to execute a set of code when
a condition is true and another if the condition is not true

elseif statement - is used with the if...else statement to execute a set of code if
one of several condition are true

switch statement - is used if you want to select one of many blocks of code to be
executed, use the Switch statement. The switch statement is used to avoid long blocks
of if..elseif..else code.

The If...Else Statement


If you want to execute some code if a condition is true and another code if a condition is
false, use the if....else statement.

Page | 15
The ElseIf Statement
If you want to execute some code if one of the several conditions is true, then use the
elseif statement.

Page | 16
The Switch Statement
If you want to select one of many blocks of code to be executed, use the Switch
statement. The switch statement is used to avoid long blocks of if..elseif..else code.

Example

Page | 17
OUTPUT

Page | 18
PHP ─ Loop Types

Loops in PHP are used to execute the same block of code a specified number of times.
PHP supports following four loop types.

for - loops through a block of code a specified number of times.


while - loops through a block of code if and as long as a specified condition is true.

do...while - loops through a block of code once, and then repeats the loop as long
as a special condition is true.

The for loop statement


The for statement is used when you know how many times you want to execute a
statement or a block of statements.

Page | 19
The while loop statement
The while statement will execute a block of code if and as long as a test expression is
true.
If the test expression is true, then the code block will be executed. After the code has
executed the test expression will again be evaluated and the loop will continue until the
test expression is found to be false.

Page | 20
The do...while loop statement
The do...while statement will execute a block of code at least once - it will then repeat
the
loop as long as a condition is true.

Page | 21
PHP ─ Strings
They are sequences of characters, like "PHP supports string operations".

There are no artificial limits on string length - within the bounds of available memory,
you ought to be able to make arbitrarily long strings.
Strings that are delimited by double quotes (as in "this") are preprocessed in both the
following two ways by PHP:

Certain character sequences beginning with backslash (\) are replaced with special
characters

Variable names (starting with $) are replaced with string representations of their
values. The escape-sequence replacements are:

\n is replaced by the newline character


\r is replaced by the carriage-return character
\t is replaced by the tab character
\$ is replaced by the dollar sign itself ($)
\" is replaced by a single double-quote (")
\\ is replaced by a single backslash (\)

Page | 22
Page | 23
PHP ─ Arrays

An array is a data structure that stores one or more similar type of values in a single
value.
For example, if you want to store 100 numbers, then instead of defining 100 variables, it
is easy to define an array of 100 length.
There are three different kind of arrays and each array value is accessed using an ID c
which is called array index.

Numeric array - An array with a numeric index. Values are stored and accessed
in linear fashion

Multidimensional array - An array containing one or more arrays and values are
accessed using multiple indices

Numeric Array
These arrays can store numbers, strings and any object but their index will be
represented
by numbers. By default, the array index starts from zero.

Page | 24
Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an array. And
each
element in the sub-array can be an array, and so on. Values in the multi-dimensional
array
are accessed using multiple index.

Example

Page | 25
Page | 26

You might also like