Your Ad Here

Sunday, January 11, 2009

PHP Programming

Q : How To Download and Install PHP for Windows?
A : The best way to download and install PHP on Windows systems is to:
which is the official Web site for PHP.
Download PHP binary version for Windows in ZIP format.
Unzip the downloaded file into a directory.

Q : Where Are PHP Configuration Settings Stored?
A : PHP stores configuration settings in a file called php.ini in PHP home directory. You can open it with any text editor to your settings.

Q : What Are the Special Characters You Need to Escape in Single-Quoted Stings?
A : There are two special characters you need to escape in a single-quote string: the single quote (') and the back slash (). Here is a PHP script example of single-quoted strings:

echo 'Hello world!';
echo 'It's Friday!';
echo ' represents an operator.';
?>

This script will print:
Hello world!It's Friday! represents an operator

Q : How To Convert Numbers to Strings in PHP?
A : In a string context, PHP will automatically convert any numeric value to a string. Here is a PHP script examples:

print(-1.3e3);
print(" ");
print(strlen(-1.3e3));
print(" ");
print("Price = $" . 99.99 . " ");
print(1 . " + " . 2 . " = " . 1+2 . " ");
print(1 . " + " . 2 . " = " . (1+2) . " ");
print(1 . " + " . 2 . " = 3 ");
print(" ");
?>

This script will print:

-1300
5
Price = $99.99
3
1 + 2 = 3
1 + 2 = 3

The print() function requires a string, so numeric value -1.3e3 is automatically converted to a string "-1300". The concatenation operator (.) also requires a string, so numeric value 99.99 is automatically converted to a string "99.99". Expression (1 . " + " . 2 . " = " . 1+2 . " ") is a little bit interesting. The result is "3 " because concatenation operations and addition operation are carried out from left to right. So when the addition operation is reached, we have "1 + 2 = 1"+2, which will cause the string to be converted to a value 1.

Q : How To Replace a Substring in a Given String in PHP?
A : If you know the position of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():

$string = "Warning: System will shutdown in NN minutes!";
$pos = strpos($string, "NN");
print(substr_replace($string, "15", $pos, 2)." ");
sleep(10*60);
print(substr_replace($string, "5", $pos, 2)." ");
?>

This script will print:

Warning: System will shutdown in 15 minutes!
(10 minutes later)
Warning: System will shutdown in 5 minutes!
Like substr(), substr_replace() can take negative starting position counted from the end of the string.

Q : How To Compare Two Strings with Comparison Operators in PHP?
A : PHP supports 3 string comparison operators, <, ==, and >, that generates Boolean values. Those operators use ASCII values of characters from both strings to determine the comparison results. Here is a PHP script on how to use comparison operators:

$a = "PHP is a scripting language.";
$b = "PHP is a general-purpose language.";
if ($a > $b) {
print('$a > $b is true.'." ");
} else {
print('$a > $b is false.'." ");
}
if ($a == $b) {
print('$a == $b is true.'." ");
} else {
print('$a == $b is false.'." ");
}
if ($a < $b) {
print('$a < $b is true.'." ");
} else {
print('$a < $b is false.'." ");
}
?>

This script will print:

$a > $b is true.
$a == $b is false.
$a < $b is false.

0 comments:

Your Ad Here