iSelfSchooling.com  Since 1999     References  |  Search more  | Oracle Syntax  | Free Online Oracle Training

    Home      .Services     Login       Start Learning     Certification      .                 .Share your BELIEF(s)...

 

. Online Accounting        .Copyright & User Agreement   |
    .Vision      .Biography     .Acknowledgement

.Contact Us      .Comments/Suggestions       Email2aFriend    |

 

What are Strings, Arrays, and Objects in PHP and how to create an Array?

What are Strings, Arrays, and Objects in PHP and how to create an Array?


A string is some combination of letters, numbers, symbols, and spaces within a pair of either single (‘) or double (“) quotation marks. If you need to use single quote in your string then use double quote to defined your string. For example: “I told, ‘Why not?’”

An array is a list of values. It uses keys to create and retrieve the values that you stored. 

An object is a class (structures) which creates instances of that structure.

The following are examples of assigning values to variables and print strings:
$my_name = ‘John’;
$my_age = 84;
$my_money = 2.95;
$my_string = ‘I asked him, “How are you?”’;
print “My name is $my_name<br /> I am $my_age years old and saved $my_money dollars”;

You may also set the variable type by casting it upon first use.
$my_name = (string) ‘John’;
$my_age = (integer) 84;

Notice that single quotation marks are treated literally and double quotation marks are extrapolated.

Creating an array: 
$list = array (‘a’,’b’,’c’,’d’);
Since we didn’t specify an index then the first item will be automatically assigned an index of 0.

$list = array (1=>’a’, 2=>’b’, 3=>’c’, 4=>’d’);
In this case the index is clear. The first item has an index 1 and so on.

$list = array (‘A’=>’a’, ‘B’=>’b’, ‘C’=>’c’, ‘D’=>’d’);
The index doesn’t need to be numeric. You can have alpha-numeric index too.

To print an array, you should use print_r($list) instead of print. Using <pre> will help in reading the elements of an array. To add just assign to its index and to delete it us unset function. For example: unset ($list[2]); or initializing an array $list = array(); or sort an array : asort($list) by values ksort($list) sort by key.

You can transform between strings and arrays by using implode() and explode() functions. You do this most of the time when you are passing array as parameter or storing it in a database. The implode() function turns an array into a string and the explode() function turns the string with comma delimiter to an array.

 

 

Google
 
Web web site