PHP learning experience

My personal learning experience on php. Basic php scripting language for beginners

Sunday, April 16, 2006

PHP Lesson - Basic Programming (1)

After talking about basic php components, such as strings, intergers, arrays, let's move on to inergrate them together and start our basic programming journey.

First, we begin with setting up conditions. Put into simply words, Conditions are expressions that PHP tests or evaluates to see whether they are true or false. Some common operator often see including:
== Are the two values equal in value?
=== Are the two values equal in both value and data type?
> Is the first value larger than the second value?
>= Is the first value larger than or equal to the second value?
< Is the first value smaller than the second value?
<= Is the first value smaller than or equal to the second value?
!== Are the two values not equal to each other in either value or data type?

PHP tests comparisons by evaluating them and returning a Boolean value, either TRUE or FALSE. For example, look at the following comparison:
$a==$b If $a=1 and $b=1, the comparison returns TRUE. If $a =1 and $b =2, the comparison
returns FALSE.

Sometimes you just need to know whether a variable exists or what type of data is in the variable. Following are some common ways to test variables:
isset($varname) # True if variable is set, even if nothing is stored in it.
empty($varname) # True if value is 0 or is a string with no characters in it or is not set.
is_array($var2): Checks to see if $var2 is an array
is_float($number): Checks to see if $number is a floating point
number
is_null($var1): Checks to see if $var1 is equal to 0
is_numeric($string): Checks to see if $string is a numeric string
is_string($string): Checks to see if $string is a string

Sometimes you need to compare character strings to see whether they fit
certain characteristics, rather than to see whether they match exact values. For example, when we gather comments from visitors, we want to make sure they are offering a valid email address. The following are some commonly used special characters that you can use in patterns.
^ Beginning of the line
$ Ending of the line
. Any single character
? Preceding character is optional
() Groups literal characters into grema string that must be matched exactly
[] A set of optional characters
- All the characters between two characters
So if we want to validate a user's mail address, we can use: ^.+@.+\.com$
The expression ^.+@.+\.com$ matches the string: johndoe@somedomain.com

Monday, April 10, 2006

PHP Learning - View and Modify Arrays

In my last lesson, I talked about how to create arrays, and we are going to continue today by starting with the topic of how to view arrays in php, then we will look into how to modify arrays in php.
You can see the structure and values of any array by using one of two
statements — var_dump or print_r. The print_r() statement, however,
gives somewhat less information. To display the $studying_day array, use
the following statement:
print_r($studying_day); and you will get the output:
Array
(
[1] => english
[2] => french
[3] => math
[4] => business
)
To get a more detailed information other than the key and value, you can use:
var_dump($studying_day);

array(3) {
[1]=>
string(7) “english”
[2]=>
string(6) “french”
[3]=>
string(4) “math”
[4]=>
string(8)"business"
}
Arrays can be changed at any time in the script, just as variables can.
Let's say I am sick of english(actually I am), thus i decided not to study english anymore, then I can use the following statement to remove it from my previously made array:
$studying_day[1] = " "; # this set the value to empty, but it does not remove
unset(studying_day[1]); # this statement completely remove it from the group
Sort Array:
One of the most useful features of arrays is that PHP can sort them for you.
To sort my array, I can use the statement: sort($studying_day);
$studying_day[1]="english";
$studying_day[2]="french";
$study_day[3]="math";
$study_day[4]="business";
To sort array that contains word as key, we use asort($arrayname); for example, we have a new array:
$computer[cpu]="AMD";
$computer[mainboard]="ESC";
$computer[keyboard]="BENQ";
$computer[screen]="LCD";
when I use asort($computer), I get the following results:
$computer[cpu]="AMD";
$computer[keyboard]="BENQ";
$computer[mainboard]="ESC";
$computer[screen]="LCD";
Some other useful sorting include:
rsort($arrayname) #Sorts by value in reverse order; assigns new numbersas the keys.
ksort($arrayname) #Sorts by key.
krsort($arrayname) #Sorts by key in reverse order.
usort($arrayname, function) #Sorts by a function.

Sunday, April 09, 2006

PHP Learning - Working with Array

Starting from today, we are going to work with arrays.
Arrays are complex variables that store a group of values under a single
variable name. An array is useful for storing a group of related values.
For example, you can store information about a computer, such as computer case, CPU, memory, and cost, in a single array named $Mycomputerinfo. Information in an array can be handled, accessed, and modified easily.
First, let's take a look at how to create arrays.
Just like createing variables, to create an array, first, we need to assign values to an array. Let's say I have a plan for studying different subject, then:
$studying_day[1]="english";
$studying_day[2]="french";
$study_day[3]="math";
$study_day[4]="business";
......
if you are crazy about studying, then you can creat along list. We observe the general formating for array is:
$arrayname[key]="value";
Array can use either numbers or strings for keys. If you don't assign any value to the key, for example, if I use $studying_day[]="english"; then the key will automatically assigned to a value of zero.
We can also use a shortcut for the above array. $studying_day(1=>"english","french","math","business");
You can also create an array with a range of values by using the following
statement:
$years = range(2001, 2010); the result looks like the following:
$years[0] = 2001
$years[1] = 2002
. . .
$years[8] = 2009
$years[9] = 2010

In my next php learning lesson, I am going to talk about how to view arrays.

Sunday, April 02, 2006

PHP Learning - Working with Data(4)

PHP provides many built-in functions for manipulating strings. When you want to remove the leading or trailing spaces, you can use the following statement:
$string = trim($string) ; # removes leading & trailing spaces
$string = ltrim($string) ; # removes leading spaces
$string = rtrim($string) ; # removes trailing spaces

PHP can also help you split a string into words. The general
form of this function is as follows:
str_word_count(“string”,format);
In this expression, format can be 1, meaning return the words as a numeric
array; or 2, meaning return the words as an array where the key is the position
of the first character of the word. It may be confusing to you at this point, I will explain array in detail in the future of my php lesson.
To give you an example here:
$string = “Counting Words”;
$number of words = str_word_count($string);
$word1 = str_word_count($string,1);
$word2 = str_word_count($string,2);

if we echo $word1, we get:
$word1[0] = Counting
$word1[1] = Words
if we echo $word2, we get:
$word2[0] = Counting
$word2[9] = Words

Saturday, April 01, 2006

PHP Learning - Working with Data(3)

Use special characters in strings:
php provides some special characters you can use in string: /n and /t
$string = “NBA \nPlayer”;
echo $string;
you will get the result :
NBA
Player

$string = “Line 1 \n\tLine 2”;
echo $string;
you will get the result:
Line 1
Line 2

These special character can only be used with double quotes, I will explain the differences between single-quoted string and double-quoted string
If you enclose a variable in double quotes, PHP uses the value of the variable.
However, if you enclose a variable in single quotes, PHP uses the literal variable. To illurstrate, see the following example:
$name ="Yao Ming";
$output1 = "$name";
$output2 = '$name';
echo $output1;
echo $output2;
you will get the result
Yao Ming
$name

Sometimes you want a character in a double-quoted string to be treated as a
literal, not as a special character, even though it has special meaning.
You can tell PHP to output characters, rather than use their special meaning, by preceding
the character with a backslash (\). This is called escaping the character.
For example, the following two strings produce the same output:
$var1="music";
$string = "The variable name is \$var1";
echo $string;
you will get the result: "The variable name is $var1" instead of "The variable name is music"