+ All Categories
Home > Technology > Introduction to php basics

Introduction to php basics

Date post: 18-Jan-2015
Category:
Upload: baabtracom-mentoring-partner-first-programming-school-in-india
View: 467 times
Download: 8 times
Share this document with a friend
Description:
 
Popular Tags:
48
Introduction to PHP
Transcript
Page 1: Introduction to php   basics

Introduction to PHP

Page 2: Introduction to php   basics

PHPPHP Hypertext Pre-processor

‚PHP is a server side scripting language‛

“Personal home page”

Page 3: Introduction to php   basics

Generate HTML

Get index.php13

4

pass index.php to PHP interpretor

5

WebServer

Index.php in interpreted HTMl form

Browser

2Get index.php from hard disk

What you mean by hypertext Pre-processor?

Page 4: Introduction to php   basics

Scripting Languages

• A ‚script‛ is a collection of program or sequence of instructions that is

interpreted or carried out by another program rather than by the computer

processor. Two types of scripting languages are

– Client-side Scripting Language

– Server-side Scripting Language

• In server-side scripting, (such as PHP, ASP) the script is processed by the

server Like: Apache, ColdFusion, ISAPI and Microsoft's IIS on Windows.

• Client-side scripting such as JavaScript runs on the web browser.

Page 5: Introduction to php   basics

What you mean by server side scripting??

Generate HTML

Get index.php13

4

pass index.php to PHP interpretor

5

WebServer

Index.php in interpreted HTMl form

Browser

2Get index.php from hard disk

PHP is server side scripting language because the php code runs on the server and returns the result in html form to the

client browser

Page 6: Introduction to php   basics

What you mean by client side scripting??

Generate HTML

Get index.php13

4

pass index.php to PHP interpretor

5

WebServer

Index.php in interpreted HTMl form

Browser

2Get index.php from hard disk

Client side scripting is something where code runs on client

side(browser). For eg checking whether a HTML text field contains data or not can be done by running

a script from browser itself

Page 7: Introduction to php   basics

Creating a PHP page

Page 8: Introduction to php   basics

How to create a PHP page

1. Install Wamp (Windows-Apache-Mysql-PHP)

2. Create a new folder within Wamp\WWW folder with the name

you want to give your project

• Eg: C:\Wamp\WWW\myProject

3. Right click and Create a new text file with extention ‚.php‛

• Eg: index.php

Page 9: Introduction to php   basics

How to run PHP program

1. Start Wamp Server

2. Open your browser and type localhost/myProject/

3. Most of the browsers load the file with name ‚index‛ automatically. So it

is recommended to have the name ‚index‛ for your home page

Page 10: Introduction to php   basics

Getting started with PHP programming

Page 11: Introduction to php   basics

PHP Syntax

Structurally similar to C/C++

Supports procedural and object-oriented paradigm (to some degree)

All PHP statements end with a semi-colon

Each PHP script must be enclosed in the reserved PHP tag

<?php

//PHP code goes here..

?>

Page 12: Introduction to php   basics

Other PHP tags

<?php

... Code

?>

Standard Tags

<?

... Code

?><?= $variable ?>

<script language=“php”>

... Code

</script>

<%

... Code

%>

Short Tags

Script TagsASP Tags

Page 13: Introduction to php   basics

Commenting

// comment single line

/* comment

multiple lines */

//comment single line

/* Comment

multiple Lines*/

# comment single line-shell style

C PHP

Page 14: Introduction to php   basics

Data Types

Page 15: Introduction to php   basics

Data types

• PHP supports many different data types, but they are generally

divided in two categories:

• Scalar

» boolean :A value that can only either be true or false

» Int :A signed numeric integer value

» float : A signed floating-point value

» string : A collection of binary data

• Composite.

» Arrays

» Objects

Page 16: Introduction to php   basics

Type Casting

echo ((0.1 + 0.7) * 10); //Outputs 8

echo (int) ((0.1 + 0.7) * 10); //Outputs 7

• This happens because the result of this simple arithmetic

expression is stored internally as 7.999999 instead of 8; when the

value is converted to int, PHP simply truncates away the fractional

part, resulting in a rather significant error (12.5%, to be exact).

Page 17: Introduction to php   basics

Variables

Page 18: Introduction to php   basics

Declaring a variable

//Declaring a variable

Int a=10;

Char c=‘a’;

Float f=1.12;

//Every variable starts with ‚$‚sign

//No need of prior Declarations

$a=10;

$c=‘a’;

$f=1.12;

C PHP

Page 19: Introduction to php   basics

Variables

• Variables are temporary storage containers.

• In PHP, a variable can contain any type of data, such as, for example,

strings, integers, floating numbers, objects and arrays.

• PHP is loosely typed, meaning that it will implicitly change the type of a

variable as needed, depending on the operation being performed on its

value.

• This contrasts with strongly typed languages, like C and Java, where

variables can only contain one type of data throughout their existence.

Page 20: Introduction to php   basics

Variable Variables

• In PHP, it is also possible to create so-called variable variables.

That is a variable whose name is contained in another variable.

For eg:

$name = ’foo’;

$$name = ’bar’;

echo $foo; // Displays ’bar’

function myFunc()

{

echo ’myFunc!’;

}

$f = ’myFunc’;

$f(); // will call myFunc();

Page 21: Introduction to php   basics

Determining If a Variable Exists

• There is an inbuilt method called isset() which will return true if a

variable exists and has a value other than null

$x=12;

echo isset ($x); // returns true

echo isset($y); // returns false

Page 22: Introduction to php   basics

Constants

Page 23: Introduction to php   basics

Declaring a constant

//Declaring a constant using ‘const’

const int a=10;

int const a=10;

//Declaring a constant using ‘#define’

#define TRUE 1

#define FALSE 0

#define NAME_SIZE 20

define(’name’, ’baabtra’);

define(’EMAIL’, ’[email protected]’);

echo EMAIL; // Displays ’[email protected]

echo name.PHP_EOL.email;

// Displays ’baabtra;

[email protected]

C PHP

Page 24: Introduction to php   basics

Constants

• Conversely to variables, constants are meant for defining immutable values.

• Constants can be accessed for any scope within a script; however, they can

only contain scalar values.

• The built-in PHP_EOL constant represents the ‚end of line‛ marker.

Page 25: Introduction to php   basics

OutPut

Page 26: Introduction to php   basics

output

Int a=10,b=25;

Printf(‚%d %d‛,a,b);

$a=10; $b=25

Printf(‚%d %d‛,$a,$b);

print $a;

print $b; //Cannot take multiple arguments

echo $a,$b; //Fast and commonly used

die(‚the end‛);

exit(‚the end‛); // both allows to terminate

the script’s output by outputting a string

C PHP

Page 27: Introduction to php   basics

echo Vs print

• echo is a language construct ( Constructs

are elements that are built-into the language

and, therefore, follow special rules)

• echo doesnt have a return value

• print() is not a language construct. It

is an inbuilt function only

• Always return 1 once it print

someting

Page 28: Introduction to php   basics

Control structures

Page 29: Introduction to php   basics

Control Structures

Conditional Control Structures

• If

• If else

• Switch

Loops

For

While

Do while

Other

• Break

• Continue

‚Exactly the same as on left side‛

C PHP

Page 30: Introduction to php   basics

functions

Page 31: Introduction to php   basics

Functions

Int findSum(int a,int b)

{

Int c;

c=a+b;

Return c

}

findSum(10,15);

function findSum($a,$b)

{

$c=$a+$b;

Return $c;

}

findSum(10,15);

C PHP

Page 32: Introduction to php   basics

Function arguments

• You can define any number of arguments and, in fact, you can pass an

arbitrary number of arguments to a function, regardless of how many you

specified in its declaration.PHP will not complain unless you provide fewer

arguments than you declared

Eg:

function greeting($arg1)

{

echo ‚hello $ arg1‛;

}

greeting(‚baabtra‛);

greeting(‚baabtra‛, ‛baabte‛);

greeting(); // ERROR

Page 33: Introduction to php   basics

Function arguments

• You can make arguments optional by giving them a default value.

function hello($who = "World"){

echo "Hello $who";}hello(‘baabtra’); // hello baabtrahello(); // hello world

Page 34: Introduction to php   basics

Function arguments

PHP provides three built-in functions to handle variable-length

argument lists:

– func_num_args() : return the total number of arguments

– func_get_arg() : return the argument at any index position

– func_get_args() : returns an array of all argument

Page 35: Introduction to php   basics

Function argumentsfunction hello(){

if (func_num_args() > 0){

$arg = func_get_arg(0); // The first argument is at position 0echo "Hello $arg";

} else {

echo "Hello World";}

}hello("Reader"); // Displays "Hello Reader"hello(); // Displays "Hello World“

Page 36: Introduction to php   basics

Function return type

• In PHP functions even if you don’t return a value, PHP will still

cause your function to return NULL. So there is no concept of

‘void’

Page 37: Introduction to php   basics

Strings

Char a*+=‚Baabtra‛;

Printf(‚Hello %s‛,a);

$a=‚Baabtra‛;

echo ‚Hello $a‛; //Output: Hello baabtra;

echo ‘Hello $a’; //Outputs : Hello $a

‚Strings will be continued in coming

chapters‛

C PHP

Page 38: Introduction to php   basics

Arrays

Indexed Array

int a[]={1,2,3,4,5,6,7,8,9,10};

for(i=0;i<10;i++)

Printf(‚%d‛,a[i]);

Indexed Array / Enumerated array

$a=array(1,2,3,4,5,6,7,8,9,10)

for($i=0;$i<10;$i++)

echo $a[$i] ;

Associative Array

$a*‘name’+=‚John‛;

$a*‘age’+=24;

$a*‘mark’+=35.65;

‚Array will be continued in coming

chapters‛

C PHP

Page 39: Introduction to php   basics

Operators

Page 40: Introduction to php   basics

Operators

C

Arithmetic Operators

+, ++, -, --, *, /, %

Comparison Operators

==, !=, ===, !==, >, >=, < , <=

Logical Operators

&&, ||, !

Assignment Operators

=, +=, -=, *=, /=, %=

PHP

‚Supports all on left . In addition

PHP supports below operators as well‛

• Execution Operators

• String operators

• Type Operators

• Error Control Operators

• Bitwise Operators

Page 41: Introduction to php   basics

• The backtick operator makes it possible to execute a shell command and

retrieve its output. For example, the following will cause the output of the

UNIX ls commandto be stored inside $a:

Eg : $a = ‘ls -l‘;

Execution operator (‘)

Don’t confuse the backtick operator with regular quotes (and, conversely, don’t

confuse the latter with the former!)

Page 42: Introduction to php   basics

– The dot (.) operator is used to concatenate two strings

Eg1: $string=‚baabtra‛. ‚mentoring parner‛

Eg2: $string1=‚baabtra‛;

$string2=‚mentoring parner‛;

$string3=$string1.$string2;

String operators (.)

Page 43: Introduction to php   basics

– Used to determine whether a variable is an instantiated object of a certain class or

an object of a class that inherits from a parent class

Eg: class ParentClass

{ }

class MyClass extends ParentClass

{ }

$a = new MyClass;

var_dump($a instanceof MyClass); // returns true as $a is an object of MyClass

var_dump($a instanceof ParentClass); // returns true as $a is an object of MyClass

which inherits parentClass

Type operators (instanceof )

Page 44: Introduction to php   basics

Error suppression operator (@)

• When prepended to an expression, this operator causes PHP to ignore

almost all error messages that occur while that expression is being evaluated

Eg: $x = @mysql_connect(‘localhost’,’root’,’1234’);

• The code above will prevent the call to mysql_connect() from outputting an

error—provided that the function uses PHP’s own functionality for

reporting errors.

• Sadly, some libraries output their errors directly, bypassing PHP and,

therefore, make it much harder to manage with the error-control operator.

Page 45: Introduction to php   basics

• Bitwise operators allow you to manipulate bits of data. All these operators are

designed to work only on integer numbers—therefore, the interpreter will attempt

to convert their operands to integers before executing them.

Bitwise Operators

& Bitwise AND. The result of the operation will be a value whose bits are set if they are set in

both operands, and unset otherwise.

| Bitwise OR. The result of the operation will be a value whose bits are set if they are set in

either operand (or both), and unset otherwise.

ˆ Bitwise XOR (exclusive OR). The result of the operation will be a value whose bits are set if

they are set in either operand, and unset otherwise.

<< Bitwise left shift. This operation shifts the left-hand operand’s bits to the left by a number of

positions equal to the right operand, inserting unset bits in the shifted positions

>> Bitwise right shift. This operation shifts the left-hand operand’s bits to the right by a number

of positions equal to the right operand, inserting unset bits in the shifted positions.

Page 46: Introduction to php   basics

Questions?

‚A good question deserve a good grade…‛

Page 47: Introduction to php   basics

End of day

Page 48: Introduction to php   basics

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]


Recommended