Showing posts with label Working. Show all posts
Showing posts with label Working. Show all posts

Tuesday, June 26, 2012

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


View the original article here

PHP learning - Working With Data(1)

In previous lesson, we have learned variables. In php, variables can store data in different types. The different types of data are:
Interger: A whole number (no fractions), such as 10, 0, -43Floating point number: A number that include decimal places, such as 5.36, 4.232Character string: A series of single characters, such as "good", "hello"Boolean: a true or false valuePhp is smarter than you think, it evaluate the variable you stored and will assign an approriate data type to the variable. For example:
$number=2 ; # php store it as interger
$sports="football" #php store it as character string

You can also tell php the type of data you want to store, look at the following example:
$number=(int) $var1; #you are telling php to store $var1 as interger
$sports=(string)$var2; #you are telling php to store $var2 as character string

To find out the type of a variable, use var_dump:
var_dump($var1) #you are asking php what type of data is variable1

PHP provides a shortcut for adding 1 to a variable, for example:
$number=$number+1; can be rewrite as $number++;
$number=$number-1; can be rewrite as $number--;
Other forms of shortcut includes:
$number+=3;
$number-=2;
$number*=4;
$number/=5;


View the original article here

PHP Learning - Working with Data(2)

Use build-in function for simple math calculations:
To compute a square root: $number=sqrt(91);
Round up to the next interger $number=ceil(27.63);
There are a lot of function help you with calculations, I will not list them here

Usually, you will want to control how to display the number, then the statement number_format comes into play.
number_format(number,decimals,"decimalsep","thousandsep)
number is the number to be formatted. This must always be included.

decimals is the number of decimal places. If decimals is not included,
the number of decimal places is 0 by default.

decimalsep is the character used to separate the decimal places. The
default is a decimal point. If you include this, you must also include
thousandsep.

thousandsep is the character used to separate the number into thousands.
The default is a comma. If you include this parameter, you must
also include decimalsep.

For more complicated formatting, you can use the statement "printf" and "sprintf".


View the original article here

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"


View the original article here

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.
View the original article here