PHP Basics and Examples
PHP Basics and Examples
Summarize this files 20% important points in order to understand the 80% then give me a deep die
with clear explanation and many examples after that give me 30 multiple choice question and 5
work out questions with answers
1. Introduction to PHP
2. PHP Basics
3. Variables in PHP
4. PHP Operators
5. Conditional Statements
6. Loops in PHP
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/13
while loop: Executes as long as a condition is true.
do...while loop: Executes at least once, then checks the condition.
for loop: Runs a fixed number of times.
foreach loop: Iterates through arrays.
7. PHP Arrays
8. PHP Functions
PHP code is written inside `<?php ?>` tags. The two main output functions are:
Example:
php
`<?php
echo "Hello, World!<br>";
print "Welcome to PHP!";
?>
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/13
2. Variables in PHP
Example:
php
`<?php
$greeting = "Hello";
$number = 10;
$floatValue = 3.14;
echo $greeting . " PHP!"; // Concatenation
?>
`
3. Operators in PHP
Arithmetic Operators
php
`<?php
$x = 10;
$y = 5;
echo $x + $y; // 15
?>
`
Comparison Operators
php
`<?php
$x = 5;
$y = "5";
var_dump($x == $y); // true
var_dump($x === $y); // false (checks type too)
?>
`
Logical Operators
php
`<?php
$a = true;
$b = false;
var_dump($a && $b); // false
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/13
?>
`
4. Conditional Statements
if...else Example
php
`<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
`
php
`<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week!";
break;
case "Friday":
echo "Weekend is near!";
break;
default:
echo "Regular day.";
}
?>
`
5. Loops
php
`<?php
$count = 1;
while ($count <= 5) {
echo $count . "<br>";
$count++;
}
?>
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/13
php
`<?php
$colors = ["Red", "Blue", "Green"];
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
`
6. Arrays
php
`<?php
$ages = ["John" => 30, "Doe" => 25];
echo "John is " . $ages["John"] . " years old.";
?>
`
7. Functions
php
`<?php
function greet($name) {
return "Hello, " . $name;
}
echo greet("John");
?>
`
8. Form Processing
Using $_POST
php
`<form method="post">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "Hello, " . $_POST['name'];
}
?>
`
9. File Handling
Reading a File
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/13
php
`<?php
$myfile = fopen("test.txt", "r");
echo fread($myfile, filesize("test.txt"));
fclose($myfile);
?>
`
Writing to a File
php
`<?php
$file = fopen("test.txt", "w");
fwrite($file, "Hello File!");
fclose($file);
?>
`
(Correct answer: b)
(Correct answer: b)
... (28 more)
1. Write a PHP script that prints numbers from 1 to 10 using a `while` loop.
2. Create an associative array with student names and marks. Print each student's name and mark.
3. Write a function that takes a number and returns whether it is even or odd.
4. Create a PHP script that reads a file and displays its contents.
5. Process a form where a user enters their age and determines if they are eligible to vote.
This structured breakdown covers the 80% understanding of PHP, followed by a deep dive into its
concepts, examples, and exercises. Would you like any modifications or additions? 😊
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/13
Multiple Choice Questions (30 Questions)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/13
9. What function is used to check if a file exists?
a) `file_exists()`
b) `exists()`
c) `check_file()`
d) `is_file()`
12. Which of the following operators is used for string concatenation in PHP?
a) `+`
b) `.`
c) `*`
d) `&`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/13
c) `fread()`
d) `fetch_file()`
18. What will `echo 5 + "5";` output?
a) `55`
b) `10`
c) `Error`
d) `"5+5"`
22. What is the correct way to check if an array contains a specific value?
a) `array_search($value, $array);`
b) `in_array($value, $array);`
c) `isset($array[$value]);`
d) `search_array($value, $array);`
25. Which function removes whitespace from the beginning and end of a string?
a) `remove_space()`
b) `trim()`
c) `strip()`
d) `cut_space()`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/13
b) `open()`
c) `fopen()`
d) `get_file()`
27. What will `intval("100.99")` return?
a) `100`
b) `100.99`
c) `"100"`
d) `101`
28. What function converts a string into an array in PHP?
a) `str_split()`
b) `explode()`
c) `split_string()`
d) `array_from_string()`
1. Write a PHP script that prints numbers from 1 to 10 using a `while` loop.
2. Create an associative array with student names and marks. Print each student's name and mark.
3. Write a function that takes a number and returns whether it is even or odd.
4. Create a PHP script that reads a file and displays its contents.
5. Process a form where a user enters their age and determines if they are eligible to vote.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/13
3. c) Both a and b
4. b) `$`
5. d) `var_dump()`
6. d) All of the above
7. c) `$_POST`
8. c) `repeat`
9. a) `file_exists()`
10. a) Checks if a variable is set and is not NULL
11. c) `.php`
12. b) `.`
13. b) `function myFunction()`
14. c) `header("Location: page.php")`
15. b) `foreach`
16. c) `;`
17. c) `fread()`
18. b) `10`
19. c) Both a and b
20. a) `session_start();`
21. a) `strlen()`
22. b) `in_array($value, $array);`
23. c) Both a and b
24. b) `HELLO`
25. b) `trim()`
26. c) `fopen()`
27. a) `100`
28. b) `explode()`
29. d) Both a and c
30. d) Both a and c
php
`<?php
$num = 1;
while ($num <= 10) {
echo $num . "<br>";
$num++;
}
?>
`
php
`<?php
$students = [
"John" => 85,
"Alice" => 90,
"Bob" => 78
];
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/13
foreach ($students as $name => $mark) {
echo "$name scored $mark <br>";
}
?>
`
php
`<?php
function checkEvenOdd($num) {
if ($num % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
php
`<?php
$filename = "test.txt";
if (file_exists($filename)) {
$file = fopen($filename, "r");
echo fread($file, filesize($filename));
fclose($file);
} else {
echo "File does not exist.";
}
?>
`
php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$age = $_POST["age"];
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/13
These answers provide a complete solution to the questions. Let me know if you need any modifications!
😊
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/13