Unit 1 - PHP Question Bank
Unit 1 - PHP Question Bank
3. Flow Control
a. If statement
b. Else statement
c. Switch statement
d. Using for Loops
e. While Loops
f. Foreach Loop
g. Skipping Iterations
4. Strings
a. String functions
b. Formatting Text Strings
c. Converting to and from Strings
5. Arrays
a. Arrays Types
b. Modifying the Data in Array
c. Deleting Array Elements
d. PHP Array functions
e. Handling array with loop
f. Sorting Arrays
g. Extracting Data from Arrays
1
Unit-I Programming in PHP ACAS
1.ESSENTIAL PHP
A)ENTER PHP
PHP Stands PHP stands for "Hypertext Preprocessor." It's recursive because
For it originally stood for "Personal Home Page."
Dynamic Web PHP enables the creation of dynamic web pages, where content
Page changes based on user interactions and data.
2
Aspect Unix/Linux Windows macOS
Apache
pre-
Use pre- installed,
installed or use
Apache, Internet Information third-party
Web Server Nginx, etc. Services (IIS) servers.
Pre-
installed,
Package or use
manager: sudo Installer package or package
Installation apt install php manual setup. manager.
Edit php.ini
file for Edit
Configuration settings. Edit php.ini file. php.ini file.
Extensions
Install via available
package via
manager or Extensions available for package
Extensions compile. download. manager.
Web Browser
Access Access via localhost or network IP.
3
Unit-I Programming in PHP ACAS
Operating File
Notes
System Text Editor / IDE Extension
Code,(komodo- nano; GUI
Linux) options.
Notepad++, Visual
Notepad++ for
Studio Code, Zend
lightweight,
Studio,
VS Code for
Maguma,komodo
features.
Windows .php
TextMate for
simplicity,
Sublime/VS
Code for
TextMate, Sublime
features.
macOS Text, VS Code .php
VS Code and
Atom are
cross-
platform;
PhpStorm is
Visual Studio
dedicated to
Cross- Code, Atom,
PHP.
Platform PhpStorm .php
4
Unit-I Programming in PHP ACAS
</head>
<body>
<h1>Welcome to my website</h1>
<?php
$age = 30;
?>
<p><?php
if ($age >= 18) {
echo "I'm an adult.";
} else {
echo "I'm not yet an adult.";
}
?></p>
<footer>
<?php echo date("Y"); ?> MyWebsite.com
</footer>
</body>
</html>
This allows you to generate dynamic content within your HTML structure.
G.PRINTING SOME TEXT:
You can use the echo statement to print text in PHP.
Example: <?php echo "This is some printed text."; ?>
The text will be sent from the server to the browser and displayed.
H. PRINTING SOME TEXT:
Put text into php files much as HTML page,put an<h1>header and textinto apage
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello Welcome!";
?>
</body>
</html>
5
Unit-I Programming in PHP ACAS
Output:
Php run from command line,in fact simply by using the php command.HTML was
not interpreted as html here,it was simply printed out as plain text.\n control
character ,which php interpret as a newline character.
Eg: echo”welcome\n”; echo”to\n”;
echo”php.”;
Syntax: To run a PHP script from the command line, use the php command followed
by the script's filename. For example: php myscript.php.
Purpose: Command line PHP is useful for running scripts, performing tasks,
automation, testing, and more without needing a web server.
6
Unit-I Programming in PHP ACAS
Variable Declaration: Declare variables using the $ symbol followed by the variable
name. Variable names are case-sensitive and must start with a letter or underscore.
$name = "John";
$age = 30;
Variable Types: PHP is loosely typed, meaning you don't need to explicitly declare variable
types. Types are determined dynamically based on the assigned value. Storing Data in
Variables in PHP:
$string = "Hello";
$number = 42;
$float = 3.14;
$boolean = true;
Concatenation: Combine strings and variables using the concatenation operator (.).
$name = "Alice";
$greeting = "Hello, " . $name . "!";
7
Unit-I Programming in PHP ACAS
Interpolation: Variables within double-quoted strings are interpolated, meaning their values
are automatically inserted.
$color = "blue";
echo "The sky is $color.";
O)Creating Constants
Using define():
define("PI", 3.14159);
define("GREETING", "Hello, world!");
Using const (Available in PHP 5.3 and later):
const PI = 3.14159;
const GREETING = "Hello, world!";
Constants are helpful for storing values that shouldn't change during the execution of your
script, such as mathematical constants, configuration settings, or messages that need to
remain consistent.
P)DATA TYPE
Data
Type Description Example
8
Unit-I Programming in PHP ACAS
Data
Type Description Example
Iterable Represents data that can be iterated over. $array = [1, 2, 3];
2)OPERATORS
Assigns a value to a
= variable. $x = 5;
Increment and
Decrement Increase or decrease the
Operators ++ and -- value of a variable by 1. $a++; // $a becomes 11
9
Unit-I Programming in PHP ACAS
3)FLOW CONTROL
low Control
Structure Definition Syntax Example
$fruits = array("apple",
foreach ($array as "banana", "orange"); foreach
foreach Iterates over elements in $value) { code to ($fruits as $fruit) { echo $fruit;
Loop an array or collection. execute; } }
These flow control structures allow you to control the execution of your PHP code based on
conditions, loops, and iterations, making your programs more dynamic and flexible.
10
Unit-I Programming in PHP ACAS
4) Strings in PHP:
a. String Functions: String functions in PHP provide a wide range of capabilities for
manipulating and working with text strings.
b. Formatting Text Strings: Formatting functions help modify and format text strings
according to specific patterns.
c. Converting to and from Strings: In PHP, you can convert other data types to strings and
vice versa.
Strings are a fundamental aspect of programming, allowing you to work with textual data in
various ways. PHP's extensive string functions empower you to manipulate and format text to
meet your specific requirements.
String Functions:
$string = "Hello, World";
11
Unit-I Programming in PHP ACAS
Arrays in PHP:
a. Array Types: Arrays in PHP are used to store multiple values in a single variable. There
are three main types of arrays:
b. Modifying the Data in Array: You can modify array elements by directly assigning
values to specific keys.
12
Unit-I Programming in PHP ACAS
d. PHP Array Functions: PHP provides various built-in functions for working with arrays,
like count(), array_push(), array_pop(), array_merge(), and more.
e. Handling Array with Loop: You can iterate through arrays using loops, such as for,
foreach, and while.
Arrays are versatile data structures in PHP that allow you to store, manipulate, and organize
multiple values efficiently. Understanding arrays and their various functions is crucial for
effective data management in your PHP programs.
13
Unit-I Programming in PHP ACAS
Definition: An indexed array is a collection of values, where each value is associated with a
numeric index.
Syntax:
$arrayName = array(value1, value2, value3, ...);
Example:
$fruits = array("apple", "banana", "orange", "grape");
In this example, the array $fruits contains four elements, and each element is associated with
a numeric index: 0, 1, 2, and 3. You can access these elements using their indices, like
$fruits[0] to get "apple".
Accessing Elements: You can access elements of an indexed array using their numeric
indices:
14
Unit-I Programming in PHP ACAS
Definition: An associative array is a collection of values, where each value is associated with
a unique key.
Syntax:
$arrayName = array(
"key1" => value1,
"key2" => value2,
"key3" => value3,
// ...
);
Example:
$person = array(
"name" => "John",
"age" => 30,
"occupation" => "Engineer"
);
In this example, the array $person has three elements, each associated with a unique key:
"name", "age", and "occupation". You can access these elements using their respective keys,
like $person["name"] to get "John".
Accessing Elements:
You can access elements of an associative array using their keys:
$name = $person["name"]; // Gets the value associated with the key "name"
$occupation = $person["occupation"]; // Gets the value associated with the key "occupation"
Modifying Elements: You can modify elements in an associative array by assigning new
values to specific keys:
$person["age"] = 31; // Modifies the value associated with the key "age"
Adding Elements:
You can add new elements to an associative array by specifying a new key:
$person["location"] = "New York"; // Adds a new key-value pair for "location"
Looping Through Associative Arrays: You can use the foreach loop to iterate through the
elements of an associative array:
foreach ($person as $key => $value) {
15
Unit-I Programming in PHP ACAS
Example:
$matrix = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
n this example, the array $matrix is a 2D array containing three arrays, each representing a
row of values. The first array [1, 2, 3] represents the first row, the second array [4, 5, 6]
represents the second row, and so on.
Accessing Elements: You can access elements of a multidimensional array by specifying
both the row and column indices:
$value = $matrix[1][2]; // Gets the value 6 from the second row and third column
16
Unit-I Programming in PHP ACAS
Modifying Elements: You can modify elements in a multidimensional array just like in
regular arrays, by assigning new values to specific indices:
$matrix[0][1] = 10; // Modifies the value at the first row and second column to 10
Adding Elements: You can add new elements to a multidimensional array by appending new
arrays to it:
$matrix[] = array(10, 11, 12); // Adds a new array as a new row
Looping Through Multidimensional Arrays: You can use nested loops to iterate through
the elements of a multidimensional array:
foreach ($matrix as $row) {
foreach ($row as $value) {
echo "$value ";
}
echo "<br>";
}
// Outputs:
// 1 2 3
// 4 5 6
// 7 8 9
// 10 11 12
17