| PHP Faqs |
|
| PHP | AJAX | MySql | OOPS | Pl/SQL |
|
| 35 |
How can I check if a value is already in an array? |
| Ans: |
If you wish to check whether a value is already stored in an array or not, then use the in array function. This is useful when you don't want any duplicates in the array and therefore only want to add a value if it's not already there. The first argument is the string you are testing for and the second is the array you are checking against.
Here is an example of in array in action:
<? php
$values = array ("sweets","namkeen","fruits","drinks");
$new value = "fruits"; |
|
|
|
If (in array ($newvalue, $values)) {echo "$new value is already in the array!";} ?>
The most common mistake made with in array is to pass the arguments in the wrong order to the function, so be careful. |
|
| 36 |
How do I sort an array by key? |
| Ans: |
The key to sorting by key is to add a k before the sort to get the function ksort.
This takes the name of the array as the parameter, and works like this:
<? php
$my world = array("c"=>"better","a"=>"make","b"=>"me");
print_r ($my world);
?>
This print:
Array
(
[c] => better
[a] => make
[b] => me
)
We need to use ksort to get the right key order:
<? php
$my world = array("c"=>"better","a"=>"make","b"=>"me"); Ksort ($my world);
print_r ($my world);
?>
Array
(
[a] => make
[b] => me
[c] => better
) |
|
| 37 |
How can you create an array of numbers easily? |
| Ans: |
If we want to create an array of consecutive numbers in PHP, then we can do it the long hand way.
<? php
$myarray = array (1, 2, 3, 4, 5, 6, 7, 8, 9);
?>
Or you can do it the easy way, using the range function, which you simply pass the first and last value in the consecutive range. So this is equivalent to the above code:
<? php
$myarray = range (1, 9);
?>
When you want an array containing the numbers 1 to a1, 000 - you'll be glad you learned about this function, so try this. |
|
| 38 |
How do I pick a random value from an array? |
| Ans: |
If you want to choose an entry randomly from an array then you can use the array_rand function like this:
<? php
$lottery = range (1, 49);
$onenumber = array_rand ($lottery);
echo "One number is: $onenumber\n\n";
?>
This will print a random number between 1 and 49.
You can also use array_rand to return a certain number of random numbers.
People often like to write a little computer program to generate their weekly lottery numbers for them. Here's how to create a lottery number generator for the USA lottery that picks six random numbers between 1 and 49 in just one line of PHP code:
<? php
for each (array_rand (range (1,49),6) as $number) echo "$number ";
?>
This will print a random selection of six numbers to the screen, for instance:
27 33 48 21 19 43. |
|
| 39 |
How do I find the size of an array? |
| Ans: |
You can find the number of values in an array using the function count, like this:
<? php
$values = range ("A","Z");
echo count ($values);
?>
This will print "26" to the screen.
Alternatively you can use size of ($array name) instead - it will return the same result. |
|
| 40 |
How do I remove the first element from an array? |
| Ans: |
Often with an array the key of the first element in the array won't simply be '0'.
Often you want to add or remove values from the start or end of an array. To remove the first value in the array and see what it is, you can use the array shift function in PHP:
<? php
$values = array(1,2,3,4,5,6,7,8,9,10);
$first = array shift ($values);
echo "First value was: $first";
print_r ($values);
?>
Which tells us this:
First value was: 1
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 10
Notice how the keys get re-assigned after the call to array shift, so that '2' now has a key of zero whereas in the initial array that key referenced the number '1' which has now been removed from the array. |
|
| 41 |
Explain differences between session_register and $_SESSION |
| Ans: |
1. session_register function returns Boolean value and $_SESSION returns string value
2. session_register function does'nt work if register_global is disabled. $_SESSION works in both case whether register_global is disabled or enabled. So using $_SESSION for session variable manipulation is more appropriate |
|
|