PHP Important Questions with Answers
1. Use of ucwords()
The ucwords() function capitalizes the first letter of each word.
Syntax:
ucwords(string $str): string
Example:
echo ucwords('hello world'); // Output: Hello World
2. a) Variable
A variable stores data and starts with $. Example:
$name = 'John'; $age = 25;
2. b) Expression
An expression is a valid PHP statement that returns a value. Example:
$sum = 5 + 10; // 15
3. extract() and compact()
extract() converts an array into variables.
Syntax:
extract(array $array);
compact() creates an array from variables.
Syntax:
compact(var1, var2, ...);
4. Sum of first 25 natural numbers
Code:
$sum = 0;
for ($i = 1; $i <= 25; $i++) {
$sum += $i;
}
echo 'Sum: ' . $sum; // 325
5. Sorting Methods
sort(), rsort(), asort(), arsort(), ksort(), krsort()
6. Use of $ and $$
$ defines a variable, $$ creates a variable from another variable name.
Example:
PHP Important Questions with Answers
$var = 'name'; $$var = 'John'; echo $name; // John
7. Area of Circle using Variable Function
Code:
function area($r) { return 3.1416 * $r * $r; }
$circleArea = 'area';
echo 'Area: ' . $circleArea(5);
8. Print 1 to 20 skipping multiples of 4
Code:
for ($i = 1; $i <= 20; $i++) {
if ($i % 4 == 0) continue;
echo "$i ";
}
9. Steps to Create PDF
1. Install FPDF
2. Include the library
3. Create an object
4. Add pages and content
5. Output PDF
10. Data Types
String, Integer, Float, Boolean, Array, Object, NULL, Resource
11. Array Traversing Methods
Using foreach and for loops:
foreach ($arr as $value) { echo $value; }
for ($i = 0; $i < count($arr); $i++) { echo $arr[$i]; }
12. Advantages of PHP
1. Open-source
2. Cross-platform
3. Easy to learn
4. Fast execution
5. Database support
13. History of PHP
PHP Important Questions with Answers
Created by Rasmus Lerdorf in 1994. PHP 3 added extensibility, PHP 4 improved performance, and PHP 7/8 enhanced
speed.
14. PHP Syntax
Code:
<?php echo 'Hello, World!'; ?>
15. Constant Variables
Constants store values that dont change. Define using define() or const.
Example:
define('SITE_NAME', 'MyWebsite');
16. Operators
Arithmetic, Assignment, Comparison, Logical, String, Bitwise, Ternary
17. Indexed vs Associative Arrays
Indexed: $arr = ['Apple', 'Banana'];
Associative: $arr = ['name' => 'John', 'age' => 25];
18. implode vs explode
implode() converts an array to string, explode() splits a string into an array.
Example:
implode(', ', ['Apple', 'Banana']) 'Apple, Banana'
explode(', ', 'Apple, Banana') ['Apple', 'Banana']