Notes.specworld.in



UNIT- I PHP

PHP is a recursive acronym for "PHP: Hypertext Preprocessor”. PHP is created in 1994 by Rasmus Lerdorf. Initially php is used for track online visitors on web. Initially it was called as Personal Home Page. PHP files can contain text, HTML, CSS, JavaScript, and PHP code. PHP files are saved with .php extension. PHP is an interpreted language, i.e. there is no need for compilation. PHP is a server side scripting language like ASP. PHP scripts are executed on the server. It is used to create dynamic web pages.It is faster than other scripting language e.g. asp and jsp.

Why to use PHP:

• It is open source and it is free downloadable

• It is plat-independent. Means- It runs on different platforms such as Windows, Linux, Unix, etc.

• This language is very simple to learn and runs efficiently on the server side.

• It is compatible with almost all servers used today,such as Apache, IIS, etc.

• It supports many databases such as MySQL, Oracle, PostgreSQL etc.

• It is perfectly suited for Web development and can be embedded directly into the HTML code.

Basic Syntax of PHP

You can run php code on any web browser. PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic Syntax of PHP:

• PHP code is start with 

• Every PHP statements end with a semicolon (;).

• PHP code can be saved with .php extension.

• PHP contain some HTML tag and PHP code.

• You can place PHP code any where in your document.

• PHP Syntax



Simple Example:

This is my first PHP code

Variable in PHP

Variable is an identifier which holds data or another one variable and whose value can be changed at the execution time of script. In PHP, a variable starts with the $ sign, followed by the name of the variable.

Syntax to declare variable in php:

$variablename=value;

Example of variable in php

Rules to declare variable in PHP

• A variable starts with the $ sign, followed by the name of the variable

• A variable name must start with a letter or the underscore character

• A variable name can't start with a number.

• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

• Variable names are case-sensitive ($name , $NAME and $Name are different variables)

Comments in PHP

Comments in any programming language is used to discribe code and make simple to understand other programmer and it is ignore by compiler or interpreter.

PHP supports single line and multi line comments. PHP comments are similar to C/C++ and Perl style (Unix shell style) comments.

PHP Single Line Comments

There are two ways to use single line comments in PHP.

• // (C and C++ style single line comment)

• # (Unix Shell style single line comment)

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */

Constant in PHP

Constants are name or identifier that can't be changed during the execution of the script. In php constants are define in two ways;

• Using define() function

• Using const keyword

In PHP declare constants follow same rules of variable declaration. Constant start with letter or underscore only.

Creating constant in php by using define() function:

Syntax

define((name, value, case-insensitive)

• name: Specifies the name of the constant

• value: Specifies the value of the constant

• case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

creating constant using cons 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.

Example of constant in php

Constants should not start with $ sign. And we should not use keywords as constant names.

Pre-defied constants:

• _LINE_ : returns The current line number of the file

• _FILE_ : returns the full path and filename of the file

• _FUNCTION_ : returns the function name

• _CLASS_ : returns the class name

• _METHOD_ : returns the class method name

• PHP_VERSION : returns the PHP version

• PHP_OS : returns the operating system

Ex: echo _LINE_; returns The current line number of the file

Printing data in PHP:

In php there are two way to get your output is; echo and print.

Difference between echo and print

echo has no return value while print has a return value of 1 so it can be used in expressions.

Datatype in PHP

PHP data types are used to hold different types of data or values. PHP supports the following data types:

• Integers − are whole numbers, without a decimal point, like 4195.

• Doubles − are floating-point numbers, like 3.14159 or 49.1.

• 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).

PHP Arrays

Arrays in PHP are a type of data structure that allows us to store multiple elements of similar data type under a single variable. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key.

In PHP An array is created using an array() function in PHP.

There are basically three types of arrays in PHP:

• Indexed or Numeric Arrays: An array with a numeric index where values are stored linearly.

• Associative Arrays: An array with a string index where instead of linear storage, each value can be assigned a specific key.

• Multidimensional Arrays: An array which contains single or multiple array within it and can be accessed via multiple indices.

➢ Indexed or Numeric Arrays

These type of arrays can be used to store any type of elements, but an index is always a number. By default, the index starts at zero. 

There are multiple ways to create arrays in PHP

1. Using array():

$colors = array("red", "green", "white");

Accessing Elements from the above array is-

echo $colors [0], "\n";

echo $colors [1], "\n";

echo $colors [2], "\n";

2. Another Way of creating Array is-

$colors [0] = " red ";

$colors [1] = " green ";

$colors [2] = " white “;

(or)

$colors [] = " red ";

$colors [] = " green ";

$colors [] = " white “;

Accessing Elements from the above array is-

echo $ colors [0], "\n";

echo $ colors [1], "\n";

echo $ colors [2], "\n";

3. Another way is using range(). But this can be used only if u have a specific range of data such as the numbers 1 to 10 (or) characters ‘a’ to ‘z’

$data = range (1, 4)

➢ Associative Arrays

These type of arrays are similar to the indexed arrays but instead of linear storage, every value can be assigned with a user-defined key of string type.

1. one way to create associative array is-

$colors = array("color1"=>"Red", "color2"=>"Green", 

                  "Color3"=>"White ");

Accessing Elements from the above array is-

echo $colors [color1], "\n";

echo $colors [color2], "\n";

echo $colors [color3], "\n";

2. Another Way of creating Array is-

$colors [color1] = " red ";

$colors [color2] = " green ";

$colors [color3] = " white “;

Accessing Elements from the above array is-

echo $colors [color1], "\n";

echo $colors [color2], "\n";

echo $colors [color3], "\n";

Modifying data in Arrays:

Ex:

• Using foreach loop:

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download