Synapseindia php programming development part2

Page 1

26.2 PHP 

Arithmetic operators −

Assignment operators  

Syntactical shortcuts Before being assigned values, variables have value undef

Constants − –

Named values define function

1


1 2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

3 4

<!-- Fig. 26.4: operators.php

5

<!-- Demonstration of operators -->

-->

operators.php (1 of 3)

6 7 8 9 10

<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Using arithmetic operators</title> </head>

11 12 13

<body> <?php

Define constant VALUE.

14

$a = 5;

15

print( "The value of variable a is $a <br />" );

16 17

// define constant VALUE

18

define( "VALUE", 5 );

Add constant VALUE to variable $a.

19 20

// add constant VALUE to variable $a

21

$a = $a + VALUE;

22

print( "Variable a after adding constant VALUE

23

is $a <br />" );

24

2


25

// multiply variable $a by 2

26

$a *= 2;

27

print( "Multiplying variable a by 2 yields $a <br />" );

Multiply variable $a by two using the multiplication assignment operator *=.

28

operators.php 50 Print if variable $a isTest lesswhether than 50.variable $a is less(2than of 3)

29

// test if variable $a is less than 50

30

if ( $a < 50 )

31

print( "Variable a is less than 50 <br />" );

32 33

// add 40 to variable $a

34

$a += 40;

35

print( "Variable a after

Add 40 to variable $a using the addition assignment operator +=. adding 40 is $a <br />" );

36 37

// test if variable $a is 50 or less

38

if ( $a < 51 )

39

print( "Variable a is still 50 or less<br />" );

40 41

// test if variable $a is between 50 and 100, inclusive

42

elseif ( $a < 101 )

43

print( "Variable a is now between 50 and 100,

44 45 46 47

inclusive<br />" ); else print( "Variable a is now greater than 100 <br />" );

48

3


49

// print an uninitialized variable

50

print( "Using a variable before initializing:

51

$nothing <br />" );

52 53

// add constant VALUE to an uninitialized variable

54

$test = $num + VALUE;

55

print( "An uninitialized variable plus constant

56

VALUE yields $test <br />" );

57

Print anAdd uninitialized variable $nothing ). constant VALUE to (an uninitialized an integer variable.

58

// add a string to

59

$str = "3 dollars";

60

$a += $str;

61

print( "Adding a string to variable a yields $a

62 63 64

operators.php (3 of 3)

<br />" ); ?> </body>

Add a string to an integer.

65 </html>

4


Fig. 26.4

26.2 PHP

Using PHP’s arithmetic operators.

5


26.2 PHP 

Keywords − Reserved for language – if…elseif…else

features

Arrays −

Group of related data 

Elements

Name plus braces and index 

Indices start at zero

function array function

– count –

6


26.2 PHP 

Arrays, cont. −

Built-in iterators 

Maintain pointer to element currently referenced

• reset • key • next • foreach loops

7


26.2 PHP PHP keywords and break case class continue default

do else elseif extends false

for foreach function global if

include list new not or

require return static switch this

true var virtual xor while

Fig. 26.5 PHP keywords.

8


1 2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

3 4

<!-- Fig. 26.6: arrays.php -->

5

<!-- Array manipulation

arrays.php (1 of 3)

-->

6 7 8 9 10

<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Array manipulation</title> </head>

11 12 13

<body>

Create the array $first by assigning a value to an array element.

<?php

14 15

// create array first

16

print( "<strong>Creating the first array</strong>

17

<br />" );

18

$first[ 0 ] = "zero";

19

$first[ 1 ] = Assign "one";

22

a value to the array, omitting the index. for loop each element’s index and value. $first[ 2 ] = Appends "two"; a Use newaelement to to theprint end out of the array. Function count returns the total number of elements in the $first[] = "three"; array.

23

// print each element’s index and value

24

for ( $i = 0; $i < count( $first ); $i++ )

20 21

25

print( "Element $i is $first[$i] <br />" );

9


26 27 28

print( "<br /><strong>Creating the second array

Call function array to create an array that contains the arguments passed to it. Store the array in variable arrays.php array to create array$second second . (2 of 3) "zero", "one", "two", "three" );

</strong><br />" );

29 30

// call function

31

$second = array(

32

for ( $i = 0; $i < count( $second ); $i++ )

33

print( "Element $i is $second[$i] <br />" );

34 35 36

print( "<br /><strong>Creating the third array </strong><br />" );

37 38

// assign values to non-numerical indices

39

$third[ "ArtTic" ] = 21;

40

$third[ "LunaTic" ] = 18;

41

$third[ "GalAnt" ] =

42

Assign values to non-numerical indices in array $third. 23; Function reset sets the internal pointer to the first element of the array.

43

// iterate through the array elements and print each

44

// element’s name and value

45

for ( reset( $third ); $element = key( $third );

46

next( $third ) )

47

print( "$element is $third[$element] <br />" );

48

Function key returns the index of the element which the internal pointer references. Function next moves the internal pointer to the next element.

10


49

print( "<br /><strong>Creating the fourth array

50

</strong><br />" );

51 52

// call function array to create array fourth using

53

// string indices

54

$fourth = array(

55

"January"

=> "first",

"February" => "second",

56

"March"

=> "third",

"April"

57

"May"

=> "fifth",

58

"July"

=>

59

"September" =>

60

"November"

61

);

=>

arrays.php (3 of 3) => "fourth",

Operator is used in function array to assign each "June" =>=> "sixth", a string index. The value to the left of the "seventh",element "August" => "eighth", is the index, and the value to the right is "ninth", operator "October" => array "tenth", the element’s=>value. "eleventh","December" "twelfth"

62 63

// print each element’s name and value

64

foreach ( $fourth as $element => $value )

65

print( "$element is the $value month <br />" );

66

?>

67

</body>

68 </html>

11


Fig. 26.6

Array manipulation.

26.2 PHP

12


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.