



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Basics of PHP where data types, operators, oops concepts, file handling and other aspects are covered
Typology: Essays (university)
1 / 59
This page cannot be seen from the preview
Don't miss anything!




















































OPEN SOURCE PROGRAMMING LANGUAGES : PHP: Introduction – Programming in web environment – variables – constants – data types – operators – Statements – Functions – Arrays – OOP – String Manipulation and regular expression – File handling and data storage.
What Can PHP Do?
PHP Features There are given many features of PHP.
All PHP code goes between php tag. Syntax of PHP tag is given below:
PHP script execute on a web server running PHP. To execute we need the following:
Variables are "containers" for storing information. Variables are used to store data, like string of text, numbers, etc. Variable values can change over the course of a script. In PHP variable can be declared as: $var_name = value;
**Rules for PHP variables:**PHP constant: const keyword
File: constant4.php
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:
PHP Data Types: Scalar Types There are 4 scalar data types in PHP.
PHP Data Types: Compound Types There are 2 compound data types in PHP.
PHP Data Types: Special Types There are 2 special data types in PHP.
PHP String A string is a sequence of characters, like "Hello world!". Strings are sequences of characters, where every character is the same as a byte.
A string can hold letters, numbers, and special characters and it can be as large as up to 2GB (2147483647 bytes maximum). The simplest way to specify a string is to enclose it in single quotes (e.g. 'Hello world!'), however you can also use double quotes ("Hello world!").
"; echo $y; ?> o/p: Hello world! Hello world!PHP Integer An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. Rules for integers:
PHP Float A float (floating point number) is a number with a decimal point or a number in exponential form. Floating point numbers (also known as "floats", "doubles", or "real numbers") are decimal or fractional numbers. In the following example $x is a float. The PHP var_dump() function returns the data type and value: Example
o/p: float(8.365)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. A resource is a special variable, holding a reference to an external resource. Resource variables typically hold special handlers to opened files and database connections
"; // Connect to MySQL database server with default setting $link = mysql_connect("localhost", "root", ""); var_dump($link); ?>The following are the most common used operators in PHP:
Arithmetic Operators The arithmetic operators require numeric values. And non-numeric values are converted automatically to numeric values. The following are the list of arithmetic operators:
Bitwise Operators Bitwise operators perform operations on the binary representation of the operands. The following illustrates bitwise operators in PHP:
Operators Name Result $x & $y And If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0 $x | $y Or (inclusive or)
If both bits are 0, the corresponding bit in the result is 0; otherwise, the corresponding bit is 1 $x ^ $y Xor (exclusive or) If either bit, but not both, in $x and $y are 1, the corresponding bit in the result is 1; otherwise, bit is 0 ~ $x Not Change bit 1 to 0 and 0 to 1 in the $x operand $x << $y Shift left Shifts the bits in $x left by the number of places specified by $y. $x >> $y Shift right Shifts the bits in $x right by the number of places specified by $y.
Comparison Operators PHP provides comparison operators to compare two operands. A comparison operator returns a Boolean value, either true or false. If the comparison is truthful, the comparison operator returns true, otherwise it returns false. The following are the list of comparison operators:
Concatenating Operator Concatenating operator (.) allows you to combine two strings into one. It appends the second string to the first string and returns the combined string. Example :
=60)? "Passed Exam":"Failed in Exam"; print "$result"; ?> **Result :** Failed in ExamArray Operator
Operator Name Description x + y Union Union of x and y x == y Equality True if x and y have the same key/value pairs x === y Identity True if x and y have the same key/value pairs in the same order and of the same types x != y Inequality True if x is not equal to y x <> y Inequality True if x is not equal to y x !== y Non-identity True if x is not identical to y
Ternary Operator Ternary operator is another conditional operator. Syntax (expr1)? (expr2) : (expr3) If the expr1 is a condition when satisfied or true it evaluates expr2, otherwise expr3.
Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does
nothing (an empty statement). Statements usually end with a semicolon. Control statements in PHP are used to control the flow of execution in a script. There
are three categories of control statements in PHP: selection statements, iteration / loop statements and jump statements.
The selection statements in PHP allows the PHP processor to select a set of statements based on the truth value of a condition or Boolean expression. Selection statements in PHP
are if, if-else, elseif ladder and switch statement.
1. The if Statement
Use the if statement to execute some code only if a specified condition is true.
The expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE – it’ll ignore it
Syntax of if is given below:
if(condition / expression) { statements(s); }
Flowchart
The following example would display ” A is bigger than B” if $a is bigger than $b:
For example, the following code would display a is bigger than b, a equal to b or a is smaller than b: $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; }?>
3. The if…elseif….else Statement
Use the if….elseif…else statement to select one of several blocks of code to be executed.
Syntax of elseif ladder is given below:
if(condition / expression) { statements(s);
elseif(condition / expression) { statements(s); } elseif(condition / expression) { statements(s); } else { statements(s); }
Example
4. Switch Statement
Syntax of switch statement is shown below:
switch(expression) { case label1: statement(s); break; case label2: statement(s); break; case label3: statement(s); break; default: statement(s);
echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?>
Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types.
Example
";> echo "Hello WorldFor Loop: The for statement is used when you know how many times you want to execute a statement or a block of statements. Syntax for ( initialization ; condition ; increment ){ code to be executed; }
Flowchart:
Example:
"; } ?>