PHP

  Home  Computer Programming  PHP


“Learn PHP programming with Interview Questions and Answers and examples.”



139 PHP Questions And Answers

1⟩ How To Download and Install PHP for Windows?

The best way to download and install PHP on Windows systems is to:

► Go to http://www.php.net, which is the official Web site for PHP.

► Download PHP binary version for Windows in ZIP format.

► Unzip the downloaded file into a directory.

You are done. No need to run any install program.

 167 views

3⟩ What Are the Special Characters You Need to Escape in Double-Quoted Stings?

There are two special characters you need to escape in a double-quote string: the double quote (") and the back slash (). Here is a PHP script example of double-quoted strings:

<?php

echo "Hello world!";

echo "Tom said: "Who's there?"";

echo " represents an operator.";

?>

This script will print:

Hello world!Tom said: "Who's there?" represents an operator.

 167 views

4⟩ Can You Specify the "new line" Character in Single-Quoted Strings?

You can not specify the "new line" character in a single-quoted string. If you don't believe, try this script:

<?php

echo ' will not work in single quoted strings.';

?>

This script will print:

will not work in single quoted strings.

How Many Escape Sequences Are Recognized in Single-Quoted Strings?

There are 2 escape sequences you can use in single-quoted strings:

► - Represents the back slash character.

► ' - Represents the single quote character.

 185 views

5⟩ How To Check Your PHP Installation?

PHP provides two execution interfaces: Command Line Interface (CLI) and Common Gateway Interface (CGI). If PHP is installed in the php directory on your system, you can try this to check your installation:

* Run "phpphp -v" command to check the Command Line Interface (CLI).

* Run "phpphp-cgi -v" command to check the Common Gateway Interface (CGI).

If you see PHP printing the version information on your screen for both commands, your installation is ok. Open a command window and run the commands below:

C:>phpphp -v

PHP 5.2.2 (cli) (built: May 2 2007 19:18:26)

Copyright (c) 1997-2007 The PHP Group

Zend Engine v2.2.0 Copyright (c) 1998-2007 Zend Technologies

C:>phpphp-cgi -v

PHP 5.2.2 (cgi-fcgi) (built: May 2 2007 19:18:25)

Copyright (c) 1997-2007 The PHP Group

Zend Engine v2.2.0 Copyright (c) 1998-2007 Zend Technologies

 163 views

6⟩ How To Download and Install PHP on Windows?

PHP is one of the most popular languages to develop dynamic Web pages. It supports all major database servers, including: MySQL, MS SQL Server, Oracle, mSQL, Sybase, etc.

If you are developing a Web application that uses PHP and needs to access MS SQL Server, you should go download and install PHP to your local machine to practice PHP and SQL Server connection. The best way to download and install PHP on Windows systems is to:

* Go to http://www.php.net, which is the official Web site for PHP.

* Click the Downloads menu link. You will see the PHP download page.

* Go to the "Windows Binaries" section, and click "PHP 5.2.3 zip package" link to download PHP binary version for Windows in ZIP format.

* Save the downloaded file, php-5.2.3-Win32.zip with 9,847,499 bytes, in C:Temp directory.

* Unzip the downloaded file into directory C:php.

You are done. No need to run any installation program.

 174 views

7⟩ How To Include Variables in Double-Quoted Strings in PHP?

Variables included in double-quoted strings will be interpolated. Their values will be concatenated into the enclosing strings. For example, two statements in the following PHP script will print out the same string:

<?php

$variable = "and";

echo "part 1 $variable part 2 ";

echo "part 1 ".$variable." part 2 ";

?>

This script will print:

part 1 and part 2

part 1 and part 2

 164 views

8⟩ How Many Escape Sequences Are Recognized in Double-Quoted Strings in PHP?

There are 12 escape sequences you can use in double-quoted strings:

► - Represents the back slash character.

► " - Represents the double quote character.

► $ - Represents the dollar sign.

► - Represents the new line character (ASCII code 10).

► - Represents the carriage return character (ASCII code 13).

► - Represents the tab character (ASCII code 9).

► { - Represents the open brace character.

► } - Represents the close brace character.

► [ - Represents the open bracket character.

► ] - Represents the close bracket character.

► nn - Represents a character as an octal value.

► xnn - Represents a character as a hex value.

 162 views

9⟩ What Are the Special Characters You Need to Escape in Single-Quoted Stings?

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:

<?php

echo 'Hello world!';

echo 'It's Friday!';

echo ' represents an operator.';

?>

This script will print:

Hello world!It's Friday! represents an operator.

 147 views

10⟩ How To Run a PHP Script?

A standard alone PHP script can be executed directly with the PHP Command Line Interface (CLI). Write the following script in a file called hello.php:

<?php echo "Hello world!"; ?>

This script can be executed by CLI interface like this:

phpphp hello.php

You should see the "Hello world!" message printed on your screen.

 151 views

11⟩ How Many Ways to Include Variables in Double-Quoted Strings in PHP?

There are 3 formats to include variables in double-quoted strings:

► "part 1 $variable part 2" - This is the simplest format to include a variable in a string. The variable name starts with the dollar sign and ends at the first character that can not be used in variable name. Space is good character to end a variable name.

► "part 1${variable}part 2" - This format helps you to clearly end the variable name. The variable name starts at dollar sign before the open brace (${) and ends at the close brace (}).

► "part 1{$variable}part 2" - This format is also called complex format. You use this format to specify any complex variable expression in the same way as in a normal statement. The variable expression starts at ({$) followed by a variable name and ends at (}).

Here is a PHP script example of different ways to include variables in double-quoted strings:

<?php

$beer = 'Heineken';

echo "$beer's taste is great. ";

echo "He drank some ${beer}s and water. ";

echo "She drank some {$beer}s and water. ";

?>

This script will print:

Heineken's taste is great.

He drank some Heinekens and water.

She drank some Heinekens and water.

 161 views

12⟩ How Many Ways to Include Array Elements in Double-Quoted Strings using PHP?

There are 2 formats to include array elements in double-quoted strings in PHP:

► "part 1 $array[key] part 2" - This is called simple format. In this format, you can not specify the element key in quotes.

► "part 1 {$array['key']} part 2" - This is called complex format. In this format, the array element expression is specified in the same way as in a normal statement.

Here is a PHP script example of different ways to include variables in double-quoted strings:

<?php

$fruits = array('strawberry' => 'red', 'banana' => 'yellow');

echo "A banana is $fruits[banana]. ";

echo "A banana is {$fruits['banana']}. ";

?>

This script will print:

A banana is yellow.

A banana is yellow.

"A banana is $fruits['banana']. " will give you a syntax error.

 164 views

13⟩ How To Access a Specific Character in a String using PHP?

Any character in a string can be accessed by a special string element expression:

► $string{index} - The index is the position of the character counted from left and starting from 0.

Here is a PHP script example:

<?php

$string = 'It's Friday!';

echo "The first character is $string{0} ";

echo "The first character is {$string{0}} ";

?>

This script will print:

The first character is It's Friday!{0}

The first character is I

 170 views

14⟩ How To Assigning a New Character in a String using PHP?

The string element expression, $string{index}, can also be used at the left side of an assignment statement. This allows you to assign a new character to any position in a string. Here is a PHP script example:

<?php

$string = 'It's Friday?';

echo "$string ";

$string{11} = '!';

echo "$string ";

?>

This script will print:

It's Friday?

It's Friday!

 180 views

15⟩ How To Concatenate Two Strings Together in PHP?

You can use the string concatenation operator (.) to join two strings into one. Here is a PHP script example of string concatenation:

<?php

echo 'Hello ' . "world! ";

?>

This script will print:

Hello world!

 166 views

16⟩ How To Compare Two Strings with Comparison Operators in PHP?

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:

<?php

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

 168 views

17⟩ How To Convert Numbers to Strings in PHP?

In a string context, PHP will automatically convert any numeric value to a string. Here is a PHP script examples:

<?php

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.

 176 views

18⟩ How To Convert Strings to Numbers in PHP?

In a numeric context, PHP will automatically convert any string to a numeric value. Strings will be converted into two types of numeric values, double floating number and integer, based on the following rules:

► The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).

► If the valid numeric data contains '.', 'e', or 'E', it will be converted to a double floating number. Otherwise, it will be converted to an integer.

 172 views

20⟩ How To Remove White Spaces from the Beginning and/or the End of a String in PHP?

There are 4 PHP functions you can use remove white space characters from the beginning and/or the end of a string:

► trim() - Remove white space characters from the beginning and the end of a string.

► ltrim() - Remove white space characters from the beginning of a string.

► rtrim() - Remove white space characters from the end of a string.

► chop() - Same as rtrim().

White space characters are defined as:

► " " (ASCII 32 (0x20)), an ordinary space.

► " " (ASCII 9 (0x09)), a tab.

► " " (ASCII 10 (0x0A)), a new line (line feed).

► " " (ASCII 13 (0x0D)), a carriage return.

► "" (ASCII 0 (0x00)), the NULL-byte.

► "x0B" (ASCII 11 (0x0B)), a vertical tab.

Here is a PHP script example of trimming strings:

<?php

$text = " Hello world! ";

$leftTrimmed = ltrim($text);

$rightTrimmed = rtrim($text);

$bothTrimmed = trim($text);

print("leftTrimmed = ($leftTrimmed) ");

print("rightTrimmed = ($rightTrimmed) ");

print("bothTrimmed = ($bothTrimmed) ");

?>

This script will print:

leftTrimmed = (Hello world!   )
rightTrimmed = (  Hello world!)
bothTrimmed = (Hello world!)

 184 views