PHP learning experience

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

Wednesday, March 29, 2006

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".

Tuesday, March 28, 2006

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:
  1. Interger: A whole number (no fractions), such as 10, 0, -43
  2. Floating point number: A number that include decimal places, such as 5.36, 4.232
  3. Character string: A series of single characters, such as "good", "hello"
  4. Boolean: a true or false value
Php 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;

Thursday, March 09, 2006

PHP Learning-Work With Constants

Constants are similiar to variables, it is created by using "define" statement, such as:
define ("Yao Ming","Best NBA Center");
a number can also be defined
define("interest", .02);

To display constants, we can use "print_r" or "echo" statement
print_r(Yao Ming);
you will get "Best NBA Center"

Constants are really easy to understand, I will talk about Data in the next php learning session. (Emm, may be you are wondering why today's lesson is so short...... Take it easy, buddy, we will go through this step by step)

Tuesday, March 07, 2006

PHP learning experience
Instruction on php scripts and share my personal php learning experience and steps, good for php beginners.

Monday, March 06, 2006

PHP Learning basics-PHP variables(1)

Basically, variables in php are containers that holds information. Variables starts with $, for example, you can name your variable $first_name, $last_name.... Be careful, variables can not start with a number, thus $4name would not be allowed in php as variable.
Some examples of variables:
$price=4;
$name="Jeff";
Something worth to notice here, when you assign a variable to a number, you don't need include "", however, when you assign variable to a string(I will explain later as we go through php in my blog), you either have to use "" or '', and a ; is always followed after you name the variable

To display a variable value, you can use "echo" or "print_r", for example:
$popsinger="Jeff Chang";
echo $popsinger ;(or print_r ($popsinger);)
For either of the command, you will get result "Jeff".
Of course, you can use "echo" command print more results once, looking at the example below:
$first_name="Jeff";
$last_name="Chang";
echo "Pop singer is", "$first_name," ", $last_name;
The result will be: Pop singer is Jeff Chang.

A little more complicated example:
$web="web";
echo "take a look at this $website";
What do we do now? we noticed that the variable "$website" does not even exist here, to tell php that we want to display website, we can use something like
echo "take a look at this {$web}site; Then you will get the desired result(It is just an example to show you something, of course i know i can just use $website="website", lol)

Look at this:
$name="NBA player";
$name="Yao Ming"
ok, why there is $ here?? if you look carefully, actually, we are creating a new variable here, which is $NBA player="Yao Ming"

To remove a variable, you can use: unset (variablename), such as unset($name), and you can unset more than one at a time, use "," to separate the variable name, such as unset($name, $NBAplayer)
Isn't that easy?? Emm... php language is not scary at all.... I think it is enough for today, we will have fun as we go through php.