pattern matching with regular expressions September 19, 2008
Posted by Nsh15 in Basic PHP programming, php.Tags: comparing string to patterns in PHP, Joining multiple comparisons in PHP, php, special characters for plttern in PHP
add a comment
Sometimes u need to compare character strings to c whether they fit certain characteristics rather than to check if they match exact values. For this type of comparison, u compare the string to a pattern, they r called regular expressions. One common use of ‘em is to check the input from a web page form.
In the table below some special characters: (all other characters r literal without any special meaning):
| character | Meaning | Example | Matched | Unmatched |
| ^ | Beginning | ^n | noor | my name is noor |
| $ | end of line | n$ | lookin$ | looking |
| . | any single character | … | ice | A |
| ? | the preceding character is optional | Noo?r | Nor, Noor | Nour |
| () | group literal characters into a string
that must be matched exactly |
N(oo)r | Noor | Nour |
| [] | enclose a set of optional literal characters | No[ou]r | Noor,Nour | Nor |
| [^] | enclose a set of non-matching optional characters | Noo[^v]r | Noor | Noovr |
| - | a range of possible | No[n-p]r | Noor, Nonr | Noar |
| + | one or more of the preceding character | Noo+r | Noooor,Nooor | Noor |
| * | zero or more of the preceding character | Nooo*r | Noor, Nooor | Norr |
| {n} | repeat n times | No{2}r | Noor | Nooor |
| {n1,n2} | specifies a range of changes | N{1-4}r | Noor | Noooooooooor |
| \ | The following character is literal (not special character) | N\*r | N*r | Noor |
| (||) | a set of alternate string | (Noor || Nour) | Noor, Nour | Sarah |
comparing string to patterns:
u can compare string to patterns using ereg:
ereg (“pattern“,value);
It will return TRUE or FALSE
Joining multiple comparisons:
U can connect as many conditions as u want in one condition statement, using one of this three words:
- and: both comparisons r true
- or: one of the comparisons is true or both r true
- xor: one of the comparisons is true but not both r true
The compression using and r tested first, then xor and at the end or r compared. If u want to change the order they r checked, simply use parentheses, its better to use parentheses even if u know the order, bcz unnecessary parentheses can’t hurt but to have unexpected result can!
Note: In some programming languages like C++ and Jave, u find || and && for (or and and in order), they work in PHP as well, they will do the same or and and functionality but || is checked before or , also && is checked before and!
Changing the order of statement execution September 19, 2008
Posted by Nsh15 in Basic PHP programming, php.Tags: Changing the order of statement execution in PHP, Checking variable content in PHP, Conditions and conditions operators in PHP, php
add a comment
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);