Open source software - PHP, Essays (university) of Computer Science

Basics of PHP where data types, operators, oops concepts, file handling and other aspects are covered

Typology: Essays (university)

2017/2018

Uploaded on 02/28/2018

.19429
.19429 🇬🇧

4

(1)

2 documents

1 / 59

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT III
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.
PHP Introduction:
PHP is an acronym for "PHP: Hypertext Pre-processor"
PHP is a server side scripting language. PHP is an open-source scripting language.
PHP is an interpreted language, i.e. there is no need for compilation.
PHP is an object-oriented language.
PHP is simple and easy to learn language.
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 Features
There are given many features of PHP.
Performance: Script written in PHP executes much faster then those scripts written
in other languages such as JSP & ASP.
Open Source Software: PHP source code is free available on the web, you can
developed all the version of PHP according to your requirement without paying any
cost.
Platform Independent: PHP are available for WINDOWS, MAC, LINUX & UNIX
operating system. A PHP application developed in one OS can be easily executed in
other OS also.
Compatibility: PHP is compatible with almost all local servers used today like
Apache, IIS etc.
Embedded: PHP code can be easily embedded within HTML tags and script.
All PHP code goes between php tag. Syntax of PHP tag is given below:
<?php
//your code here
?>
Programming in Web Environment:
PHP script execute on a web server running PHP. To execute we need the following:
The Apache Web server
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b

Partial preview of the text

Download Open source software - PHP and more Essays (university) Computer Science in PDF only on Docsity!

UNIT III

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.

PHP Introduction:

  • PHP is an acronym for "PHP: Hypertext Pre-processor"
  • PHP is a server side scripting language. PHP is an open-source scripting language.
  • PHP is an interpreted language, i.e. there is no need for compilation.
  • PHP is an object-oriented language.
  • PHP is simple and easy to learn language.

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 Features There are given many features of PHP.

  • Performance: Script written in PHP executes much faster then those scripts written in other languages such as JSP & ASP.
  • Open Source Software: PHP source code is free available on the web, you can developed all the version of PHP according to your requirement without paying any cost.
  • Platform Independent : PHP are available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP application developed in one OS can be easily executed in other OS also.
  • Compatibility: PHP is compatible with almost all local servers used today like Apache, IIS etc.
  • Embedded: PHP code can be easily embedded within HTML tags and script.

All PHP code goes between php tag. Syntax of PHP tag is given below:

Programming in Web Environment:

PHP script execute on a web server running PHP. To execute we need the following:

  • The Apache Web server
  • The PHP engine
  • The MySQL database server

PHP Variables

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:**
  • All variables in PHP start with a $ 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 in PHP can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
  • A variable name cannot contain spaces.
  • (^) Variable names in PHP are case sensitive, it means $x and $X are two different variables settype Function in PHP PHP settype function is used to set variable type to a specified variable. It returns the value of specified type on successful type assignment and 0 on failure. Syntax: settype(var_name, var_type) var_name The variable being converted. var_type Type of the variable. Possible values are : boolean, integer, float, string, array, object, null. Example:
", $val2."
"; echo $val3."
"; ?>

PHP constant: const keyword

  • The const keyword defines constants at compile time. It is a language construct not a function.
  • It is bit faster than define().
  • It is always case sensitive.

File: constant4.php

PHP Data Types

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:

  1. (^) Scalar Types
  2. Compound Types
  3. Special Types

PHP Data Types: Scalar Types There are 4 scalar data types in PHP.

  1. boolean
  2. integer
  3. float
  4. string

PHP Data Types: Compound Types There are 2 compound data types in PHP.

  1. Array 2. object

PHP Data Types: Special Types There are 2 special data types in PHP.

  1. Resource 2. NULL

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:

  • 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) optionally preceded by a sign (- or +). The PHP var_dump() function returns the data type and value:
o/p: int(85)

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)

o/p: NULL

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); ?>

PHP Operators

The following are the most common used operators in PHP:

  1. Arithmetic Operators 2. Assignment Operators
    1. Bitwise Operators 4. Comparison Operators
    2. Increment/Decrement Operators 6. Logical Operators
    3. Concatenating Operators 8. Conditional Operators
    4. Array Operator

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:

  • Addition ( +) returns the sum of two operands
  • Subtraction ( -) returns the difference between two operands
    • Multiplication ( *) return the product of two operands
    • Division ( /) returns the quotient of two operands. The result of division of two integers may produce an integer ( 4/2 = 2) or a floating-point number ( 3/2 = 1.5)
  • Modulus ( %) returns the remainder of the division of the first operand by the second operand. Both operands are integers. Operator Name Description x + y Addition Sum of x and y x - y Subtraction Difference of x and y x * y Multiplication Product of x and y x / y Division Quotient of x and y x % y Modulus Remainder of x divided by y
  • x Negation Opposite of x a.b Concatenation Concatenate two strings Assignment Operators Assignment operator ( =) assigns a value to a variable and returns a value. The operand on the left is always a variable, while the operand on the right can be a literal value, variable, expression or function call that returns a value..Besides the basic assignment operator( =), PHP provides some assignment operators: *plus-equal += minus-equal -= divide-equal /= multiply-equal = modulus-equal %= XOR-equal ^= AND-equal &= OR-equal |= concatenate-equal .=

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:

  • Equality ( ==) returns true if both operands are equal, otherwise returns false.
    • Identity ( ===) return true if both operands have the same data type and equal, otherwise return false.
  • Inequality ( != or <>) returns true if both operands are not equal, otherwise return false.
  • Not identical ( !===) returns true if both operands are not equal or not have the same data type, otherwise returns false.
  • Logical negation ( !) returns true if the operand is false , and returns false if the operand is true. Operator Name Description x and y And True if both x and y are true x or y Or True if either or both x and y are true x xor y Xor True if either x or y is true, but not both x && y And True if both x and y are true x y Or True if either or both x and y are true ! x Not True if x is not true

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 Exam

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

Statements in PHP

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

selects one of many blocks of code to be executed

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.

  • 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.
  • foreach − loops through a block of code for each element in an array. while Loop 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. It should be used if number of iteration is not known. Syntax: while ( condition ) { code to be executed ; } Flowchart:

Example

";> echo "Hello World
"; $x1=$x1+1; } ?>

For 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:

"; } ?>