| Author |
Message |
geneticthylon
Started Website
Posts: 23
Group: Newbie
Joined: Jul 2008
Status:
Offline
Reputation: 0
|
|
| 07-29-2008 01:27 PM |
|

|
geneticthylon
Started Website
Posts: 23
Group: Newbie
Joined: Jul 2008
Status:
Offline
Reputation: 0
|
|
| 07-29-2008 01:47 PM |
|
 |
geneticthylon
Started Website
Posts: 23
Group: Newbie
Joined: Jul 2008
Status:
Offline
Reputation: 0
|
RE: PHP tutorial
Lesson 4 : Variable Types
PHP supports eight primitive types.
Four scalar types:
* boolean : expresses truth value, TRUE or FALSE. Any non zero values and non empty string are also counted as TRUE.
* integer : round numbers (-5, 0, 123, 555, ...)
* float : floating-point number or 'double' (0.9283838, 23.0, ...)
* string : "Hello World", 'PHP and MySQL, etc
Two compound types:
* array
* object
And finally two special types:
* resource ( one example is the return value of mysql_connect() function)
* NULL
In PHP an array can have numeric key, associative key or both. The value of an array can be of any type. To create an array use the array() language construct like this.

When working with arrays there is one function I often used. The print_r() function. Given an array this function will print the values in a format that shows keys and elements

Don't forget to print the preformatting tag <pre> and </pre> before and after calling print_r(). If you don't use them then you'll have to view the page source to see a result in correct format.
Type Juggling
In PHP you don't need to explicitly specify a type for variables. A variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable $var, $var becomes a string. If you then assign an integer value to $var, it becomes an integer.
An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.
Example :

Type Casting
To cast a variable write the name of the desired type in parentheses before the variable which is to be cast.

The casts allowed are:
* (int), (integer) - cast to integer
* (bool), (boolean) - cast to boolean
* (float), (double), (real) - cast to float
* (string) - cast to string
* (array) - cast to array
* (object) - cast to object
|
|
| 07-29-2008 02:08 PM |
|
 |
geneticthylon
Started Website
Posts: 23
Group: Newbie
Joined: Jul 2008
Status:
Offline
Reputation: 0
|
RE: PHP tutorial
Lesson 5 : Playing With Strings
Strings are probably what you will use most in your PHP script. From concatenating, looking for patterns, trim, chop etc. So I guess it's a good idea to take a better look at this creature. We will also take a peek at some string functions that you might find useful for everyday coding.
Creating a string
To declare a string in PHP you can use double quotes ( " ) or single quotes ( ' ). There are some differences you need to know about using these two.
If you're using double-quoted strings variables will be expanded ( processed ). Special characters such as line feed ( \n ) and carriage return ( \r ) are expanded too. However, with single-quoted strings none of those thing happen. Take a look at the example below to see what I mean.
Note that browsers don't print newline characters ( \r and \n ) so when you open string.php take a look at the source and you will see the effect of these newline characters.

String Concatenation
To concat two strings you need the dot ( . ) operator so in case you have a long string and for the sake of readability you have to cut it into two you can do it just like the example below.
Actually if you need to write a loong string and you want to write it to multiple lines you don't need concat the strings. You can do it just like the second example below where $quote2 is split into three lines.

String Functions
substr($string, $start, $end) : get a chunk of $string

str_repeat($string, $n) : repeat $string $n times
For example if you want to print a series of ten asteriks ( * ) you can do it with a for loop like this :

Or you can go the easy way and do it like this :

strrchr($string, $char) : find the last occurence of the character $char in $string
For example: you want to get the file extension from a file name. You can use this function in conjunction with substr()

What the above code do is get a chunk of $filename starting from the last dot in $filename then get the substring of it starting from the second character ( index 1 ).
To make things clearer suppose $filename is 'tutorial.php'. Using strrchr('tutorial.php', '.') yield '.php' and after substr('.php', 1) we get the file extension; 'php'
trim($string) : remove extra spaces at the beginning and end of $string

addslashes($string) : adding backslashes before characters that need to be quoted in $string
This function is usually used on form values before being used for database queries. You will see this function used a lot in this tutorial so there's no need to present an example here.
explode($separator, $string) : Split $string by $separator
This function is commonly used to extract values in a string which are separated by a a certain separator string. For example, suppose we have some information stored as comma separated values. To extract each values we ca do it like shown below

Now, $info is an array with three values :
Array
(
[0] => Uzumaki Naruto
[1] => 15
[2] => Konoha Village
)
We can further process this array like displaying them in a table, etc.
implode($string, $array) : Join the values of $array using $string
This one do the opposite than the previous function. For example to reverse back the $info array into a string we can do it like this :

Another example : Pretend we have an array containing some values and we want to print them in an ordered list. We can use the implode() like this :

The result of that code is like an ordered list just like shown below
1. Uchiha Sasuke
2. Haruno Sakura
3. Uzumaki Naruto
4. Kakashi
By the way, i did write the above php code to print that list instead of writing the list directly
number_format($number): display a number with grouped thousands
When displaying numbers it's usuallly more readable if the numbers is properly formatted like 1,234,567 instead of 1234567.
|
|
| 07-29-2008 03:27 PM |
|
 |
geneticthylon
Started Website
Posts: 23
Group: Newbie
Joined: Jul 2008
Status:
Offline
Reputation: 0
|
|
| 07-29-2008 03:43 PM |
|
 |
geneticthylon
Started Website
Posts: 23
Group: Newbie
Joined: Jul 2008
Status:
Offline
Reputation: 0
|
|
| 07-29-2008 03:58 PM |
|
 |
geneticthylon
Started Website
Posts: 23
Group: Newbie
Joined: Jul 2008
Status:
Offline
Reputation: 0
|
|
| 07-29-2008 04:08 PM |
|
 |