jump to navigation

Changing the order of statement execution September 19, 2008

Posted by Nsh15 in Basic PHP programming, php.
Tags: , , ,
trackback

PHP scripts r series of instructions in a file and they r executed one after another from the beginning (top of the file) to the end.

To change the flow of the script u need to add complex statements, PHP provides two types of ‘em:

  • Conditional statements: U set up statements that execute only if a certain conditions r met. (If statement and switch statement)
  • Looping statements: U can execute a statements repeatedly; this is called looping. (for, while and do… while statements)

Both types of complex statements execute a block of statements based on a condition. That is if the condition is true in the “Conditional statements” then the statements will execute once but  if the condition is true in the “Looping statements” then the statements will execute till the condition become not true.

Conditions and conditions operators:

Conditions r expressions that PHP tests or evaluates to c whether they are TRUE or FALSE, to set up conditions u compare values.

U can use several conditions operators, and they r listed in the table below:

Operator It’s use
== to compare two values
=== to compare two values in both values and data type
> To compare if the first value(to the left) is larger than the second value (to the right)
>= To compare if the first value(to the left) is equal or larger than the second value (to the right)
< To compare if the first value(to the left) is less than the second value (to the right)
<= To compare if the first value(to the left) is equal or less than the second value (to the right_
!= or <> returns true if the two values r not equal
!== returns true if the two values r not equal in their value or in their data type

note1: U can compare both numbers and strings, strings r compared according to their ASCII value.

note2: Pay attention when u want to compare values to use double equal == not single equal, coz the single equal will assign the value and it will always return TRUE. i.e: $a==3; (compression) but $a=3; (a will be equal 3 after executing this statement).

Checking variable content:

there is some special test provided by PHP for variables, such as:

Test Explanation variable data type
isset($variable_name); TRUE if the variable is set even if nothing is stored in it variable
empty($variable_name); TRUE if:1. variable is not set

2.value =0

3. no character for string values

variable
is_int($variable_name); TRUE if the data type for the variable is integer number
is_array($variable_name); TRUE if the variable is array variable
is_float($variable_name); TRUE if the data type for the variable is floating point number
is_null($variable_name); TRUE if the value =0 in the variable variable
is_numeric($variable_name); TRUE if the string inside the variable is numeric string string
is_string($variable_name); TRUE if the variable is string string

u can also test for the negative for any of the above tests by adding ! before it.

i.e.. if u want the reuslt to be FALSE when the variable is set use !isset($variable_name);

Advertisement

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.