160 Questions
What is the PHP style comment that is similar to C/C++ style comment?
//
What is the PHP comment that is similar to Unix Shell style comment?
What is the purpose of PHP if statement?
To test a condition and execute a block of code if true
What is the syntax of PHP if statement?
if(condition) { code to be executed }
What is the purpose of PHP if-else statement?
To test a condition and execute a block of code if true or false
What is the purpose of PHP switch statement?
To execute one statement from multiple conditions
What is the syntax of PHP switch statement?
switch(expression) { case value1: code to be executed }
What is the purpose of break statement in PHP switch statement?
To terminate the switch statement
What is the purpose of the default block in a PHP switch statement?
To execute code when no cases are matched
What is the syntax of a PHP for loop?
for(initialization; condition; increment/decrement) {}
When should we use a PHP while loop instead of a for loop?
When the number of iterations is unknown
What is the purpose of a PHP nested for loop?
To execute code for each iteration of the outer loop
How many times will the inner for loop be executed in a nested for loop if the outer loop is executed 3 times and the inner loop is executed 3 times?
9 times
What is the purpose of a PHP for each loop?
To traverse an array
What is the output of the PHP switch statement with a default block?
Number is equal to 20
What is the purpose of the initialization block in a PHP for loop?
To initialize the loop counter
How many ways can you define an associative array in PHP?
Two
What is the default representation of elements in a PHP indexed array?
Index Number
What is the function used to count the length of a PHP indexed array?
count()
How do you traverse a PHP indexed array?
Using foreach loop
What is the symbol used to associate a name/label with each array element in PHP?
=>
What type of array can store numbers, strings or any object?
Indexed array
What is another name for a PHP indexed array?
Numeric array
What is the output of the count() function when used with an indexed array?
The length of the array
What does the decoct() function convert a decimal number into?
Octal
What is the purpose of the base_convert() function?
To convert any base number to any base number
What does the bindec() function convert into decimal?
Binary number
What is the return type of the dechex() function?
String
What is the purpose of the dechex() function?
To convert decimal to hexadecimal
What is the return type of the decoct() function?
String
What is the purpose of the bindec() function?
To convert binary to decimal
What is the syntax of the base_convert() function?
string base_convert ( string $number , int $frombase , int $tobase )
What is one of the benefits of using PHP functions?
It saves a lot of code
How do you declare a user-defined function in PHP?
function functionname(){ //code to be executed }
What is the purpose of arguments in PHP functions?
To pass the information in the PHP function
What is the default passing method in PHP function arguments?
Call by Value
What is the purpose of parameterized functions in PHP?
To pass any number of parameters inside a function
What is the output of a parameterized function dependent on?
The dynamic values passed as parameters
What is the advantage of PHP functions in terms of code organization?
It separates the programming logic
What is the rule for naming a function in PHP?
It must start with a letter or underscore
Which PHP statement is used to print a string?
echo
What is the return type of the PHP print statement?
int
What can be printed using the PHP echo statement?
Strings, multi-line strings, escaping characters, and variables
What is the purpose of a variable in PHP?
To hold a memory location that stores data
What is similar between PHP echo and PHP print?
Both are language constructs
What is the difference between PHP echo and PHP print?
echo returns void, while print always returns 1
Can PHP print be used to print arrays?
Yes
What is the syntax of the PHP print statement?
int print(string $arg)
How many primitive data types does PHP support?
8
What is the category of data type that includes arrays and objects?
Compound Types
What type of operator works on three operands?
Ternary Operator
What is the purpose of comments in PHP?
To describe code for other developers
How many categories of operators can PHP operators be categorized into based on operands?
3
What type of data type is a resource in PHP?
Special Type
How many scalar data types are there in PHP?
4
What type of operator is the '+' symbol in the expression $num = 10 + 20?
Binary Operator
What is the purpose of a PHP foreach loop?
To traverse a set of code like a while loop
What is the syntax of a PHP while loop?
while(condition) { //code to be executed }
What is a nested while loop in PHP?
A loop inside another loop
What is the purpose of a PHP do-while loop?
To traverse a set of code like a while loop
What is the output of the example PHP while loop?
1 to 10
How many times will the inner while loop be executed if the outer loop is executed 3 times and the inner loop is executed 3 times?
9 times
What is the example output of a PHP foreach loop?
Season is: summer Season is: winter
What is the purpose of using a foreach loop in PHP?
To traverse an array or object
What is the purpose of PHP array() function?
To create and return an array
What is the purpose of PHP array_change_key_case() function?
To change the case of all keys of an array
What does PHP array_chunk() function do?
Splits an array into chunks
What is the output of the given PHP code: array('sonoo' => 400000, 'john' => 500000, 'rahul' => 300000)?
Array ( [sonoo] => 400000 [john] => 500000 [rahul] => 300000 )
What type of array is created by the PHP code: array(400000, 500000, 300000)?
Indexed array
What is the function used to count the length of a PHP array?
count()
How can you traverse a PHP indexed array?
All of the above
What is the symbol used to associate a name/label with each array element in PHP?
=>
What is the purpose of the default block in a PHP switch statement?
To specify the code to be executed when all cases are not matched
What is the syntax of a PHP for loop?
for(initialization; condition; increment/decrement){}
What is the purpose of a PHP for each loop?
To traverse array elements
What is the output of the PHP for loop example?
1, 2, 3, 4, 5, 6, 7, 8, 9
What is the purpose of the initialization block in a PHP for loop?
To specify the starting value for the loop
What happens in a PHP nested for loop?
The inner loop is executed fully for one outer loop
What is the output of the PHP nested for loop example?
11, 12, 13, 21, 22, 23, 31, 32, 33
When should a PHP while loop be used instead of a for loop?
When the number of iterations is not known
What does PHP stand for?
Hypertext Preprocessor
What is the primary requirement for web development using PHP?
Basic knowledge of HTML
What is the file extension used to save a PHP file?
.php
Why is PHP is faster than other scripting languages?
Because it is an interpreted language
What is the purpose of PHP in web development?
To create dynamic websites
What software stack is recommended to install PHP?
AMP (Apache, MySQL, PHP)
What is the category of data type that includes arrays and objects?
Non-primitive data type
What is the purpose of PHP in web development besides creating dynamic websites?
To handle form data and to interact with databases
What is the main difference between PHP echo and PHP print?
PHP echo always returns 1, while PHP print does not return a value
What is the purpose of a variable in PHP?
To hold a memory location that stores data
What can be printed using the PHP print statement?
Strings, multi-line strings, escaping characters, and variables
What is the return type of the PHP print statement?
Always returns 1
Why are PHP functions beneficial in terms of code organization?
They allow for code reuse and modularity
What is similar between PHP echo and PHP print?
They are both used for printing output
What is the syntax of the PHP print statement?
int print(string $arg)
Can PHP print be used to print arrays?
Yes, PHP print can be used to print arrays
What is the return type of the strlen function?
Integer
What does the abs function return for a floating-point value?
Float
What is the purpose of the ceil function?
To round fractions up
What is the return type of the sqrt function?
Float
What does the decbin function convert a decimal number into?
Binary
What is the purpose of the floor function?
To round fractions down
What is the return type of the dechex function?
String
What is the purpose of the sqrt function?
To return the square root of a number
What does the PHP strtolower()
function do?
Returns string in lowercase letter
What is the primary difference between a do-while loop and a while loop in PHP?
The code is executed at least once in a do-while loop.
What is the output of the ucfirst()
function with the input 'my name is anuj'?
My name is ANUJ
What does the PHP strrev()
function do?
Reverses the string
What is the purpose of the PHP break statement?
To break the execution of the inner loop only.
What is the purpose of the PHP strlen()
function?
Returns length of the string
What is an advantage of using PHP functions?
Code Reusability
What is the purpose of a PHP function?
To take input as an argument list and return a value.
What does the PHP lcfirst()
function do?
Converts first character into lowercase
What is the purpose of the PHP ucwords()
function?
Converts first character of each word into uppercase
What is the format of a PHP do-while loop?
do { code } while (condition);
What is the output of the strtoupper()
function with the input 'my name is anuj'?
MY NAME IS ANUJ
What happens when the PHP break statement is used inside a nested loop?
The execution of the inner loop is broken.
What is the characteristic of a PHP function that allows it to be reused?
It can be defined only once and invoked many times.
What does the PHP strtolower()
function return?
String in lowercase letter
What is the benefit of using PHP functions in terms of code organization?
It allows for code reusability.
How many primitive data types does PHP support?
8
What are the categories of PHP data types?
Scalar, Compound, Special
What type of operator works on three operands?
Ternary Operator
What is the purpose of PHP comments?
To describe code and hide other code
How many types of comments does PHP support?
2
What is the category of data type that includes resource and NULL?
Special Type
What type of operator works on single operands?
Unary Operator
How many categories of operators are there in PHP based on the number of operands?
3
What is the purpose of PHP multidimensional array?
To store tabular data in an array
How many ways can you define an associative array in PHP?
Two ways
What is the symbol used to associate a name/label with each array element in PHP?
=
What is another name for a PHP indexed array?
Numeric array
What is the purpose of an associative array in PHP?
To store data with a key-value pair
What is represented by a PHP multidimensional array?
All of the above
In which form is a PHP multidimensional array represented?
Row * Column
What type of array can store numbers, strings, or any object in PHP?
Array
Which of the following PHP comments is similar to Unix Shell style comment?
What is the purpose of the PHP if-else statement?
To execute a block of code if the condition is true or false
What is the purpose of the PHP switch statement?
To execute one statement from multiple conditions
What is the syntax of the PHP if statement?
if(condition) { code to be executed }
What is the purpose of the break statement in PHP switch statement?
To stop executing the switch statement
What is the syntax of the PHP if-else statement?
if(condition) { code to be executed } else { code to be executed }
What is the purpose of the PHP multi-line comments?
To comment multiple lines of code
What is the output of the PHP if statement if the condition is true?
The code to be executed if the condition is true
What is the return type of the PHP abs() function?
integer
What does the PHP ceil() function do?
Rounds fractions up
What is the purpose of the PHP decbin() function?
Converts decimal to binary
What is the return type of the PHP sqrt() function?
float
What does the PHP floor() function do?
Rounds fractions down
What is the purpose of the PHP strlen() function?
Returns the length of a string
What is the return type of the PHP dechex() function?
string
What does the PHP sqrt() function return?
The square root of a given argument
What is the advantage of using POST request over GET request?
More secure as data is not visible in the URL
What is the purpose of PHP include and require constructs?
To achieve code reusability in PHP scripts
What is the difference between PHP include and require?
Include allows the script to continue if the file is missing, while require halts the script
What is the purpose of a PHP file extension .html?
To denote an HTML file that can be included in a PHP script
What is the output of the PHP script if a required file is missing?
A fatal E_COMPILE_ERROR level error
What is the benefit of using PHP include and require constructs?
Code reusability and reduced script size
What type of file can be included using PHP include and require constructs?
Both PHP and HTML files
What is the purpose of including a file in a PHP script?
To reuse code in multiple PHP scripts
Study Notes
PHP Comments
- There are two ways to use single line comments in PHP:
//
(C++ style) and#
(Unix Shell style) - PHP also supports multi-line comments, which are enclosed within
/*
and*/
PHP Control Statements
- PHP if-else statement is used to test a condition and execute different blocks of code based on the condition
- There are various ways to use if statements in PHP:
if
,if-else
,if-else-if
, andnested if
- The syntax for an
if
statement isif (condition) { code to be executed }
- The syntax for an
if-else
statement isif (condition) { code to be executed if true } else { code to be executed if false }
- PHP also has a
switch
statement, which is used to execute one statement from multiple conditions - The syntax for a
switch
statement isswitch (expression) { case value1: code to be executed; break; ... default: code to be executed if all cases are not matched; }
PHP Loops
- PHP has several types of loops:
for
,foreach
, andwhile
- The
for
loop is used to execute a block of code for a specified number of times - The syntax for a
for
loop isfor (initialization; condition; increment/decrement) { code to be executed }
- PHP also supports nested
for
loops - The
foreach
loop is used to traverse array elements - PHP has a
foreach
loop for arrays, which is used to traverse array elements
PHP Functions
- PHP allows you to declare and call user-defined functions easily
- The syntax for declaring a user-defined function is
function functionname() { code to be executed }
- PHP functions support arguments, which are passed to the function when it is called
- PHP supports call by value, call by reference, default argument values, and variable-length argument lists
- PHP parameterized functions are functions with parameters
PHP Arrays
- PHP has two types of arrays: indexed arrays and associative arrays
- Indexed arrays are represented by an index number, which starts from 0
- Indexed arrays can store numbers, strings, or any object
- Associative arrays are represented by a key-value pair
- PHP provides the
count()
function, which returns the length of an array - You can easily traverse an array in PHP using a
foreach
loop
PHP Math Functions
- PHP has several math functions, including
dechex()
,decoct()
,base_convert()
, andbindec()
- The
dechex()
function converts a decimal number to a hexadecimal number - The
decoct()
function converts a decimal number to an octal number - The
base_convert()
function converts a number from one base to another - The
bindec()
function converts a binary number to a decimal number
PHP Echo
- Prints a string
- Can print multi-line strings
- Escapes characters
- Can print variable values
PHP Print
- Similar to PHP echo
- Always returns 1
- Syntax:
int print(string $arg)
- Can print strings, multi-line strings, escaping characters, and variables
PHP Variables
- A variable is a name of a memory location that holds data
- Supports 8 primitive data types:
- Scalar Types: boolean, integer, float, string
- Compound Types: array, object
- Special Types: resource, NULL
PHP Data Types
- Scalar Types:
- boolean
- integer
- float
- string
- Compound Types:
- array
- object
- Special Types:
- resource
- NULL
PHP Operators
- Can be categorized into:
- Arithmetic Operators
- Comparison Operators
- Bitwise Operators
- Logical Operators
- String Operators
- Incrementing/Decrementing Operators
- Array Operators
- Type Operators
- Execution Operators
- Error Control Operators
- Assignment Operators
- Can also be categorized into:
- Unary Operators (works on single operands)
- Binary Operators (works on two operands)
- Ternary Operators (works on three operands)
PHP Comments
- Used to describe code
- Can be single-line or multi-line
- Helps other developers understand the code
PHP Switch
- Used for decision-making
- Syntax:
switch (expression) { case value: code; break; ... default: code; }
PHP Loops
- PHP For Loop:
- Used to traverse a set of code for a specified number of times
- Syntax:
for (initialization; condition; increment/decrement) { code to be executed }
- PHP Nested For Loop:
- Used for inner or nested for loops
- Inner for loop is executed fully for one outer for loop
- PHP For Each Loop:
- Used to traverse array elements
- Syntax:
foreach ($array as $var) { code to be executed }
- PHP While Loop:
- Used to traverse a set of code
- Syntax:
while (condition) { code to be executed }
- PHP Nested While Loop:
- Used for inner or nested while loops
- Inner while loop is executed fully for one outer while loop
- PHP Do While Loop:
- Used to traverse a set of code like while loop
- Syntax:
do { code to be executed } while (condition)
PHP Array Functions
- PHP array() function:
- Creates and returns an array
- Allows creation of indexed, associative, and multidimensional arrays
- PHP array_change_key_case() function:
- Changes the case of all keys in an array
- Syntax:
array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )
- PHP array_chunk() function:
- Splits an array into chunks
- Divides an array into many parts
Introduction to PHP
- PHP stands for HyperText Preprocessor
- It's an interpreted language, no compilation is needed
- PHP is a server-side scripting language
- PHP is faster than other scripting languages like ASP and JSP
PHP Examples
- PHP files must be saved with a
.php
extension - A simple PHP example is shown with a
hello.php
file - Output of the example: "Hello by PHP"
Web Development
- PHP is widely used in web development
- Dynamic websites can be developed using PHP
- Basic knowledge of HTML, CSS, JavaScript, AJAX, XML, and JSON is required for web development
PHP Echo
-
echo
is a language construct in PHP - It's used to print strings, multi-line strings, escaping characters, and variable values
- Examples of
echo
usage are shown inecho1.php
,echo2.php
, andecho3.php
files - Output of examples: "Hello by PHP echo", "Hello by PHP echo this is multi line text printed by PHP echo statement", and "Hello escape "sequence" characters"
PHP Print
-
print
is a language construct in PHP - It's used to print strings, multi-line strings, escaping characters, and variable values
- Unlike
echo
,print
always returns 1 - Examples of
print
usage are shown inprint1.php
,print2.php
, andprint3.php
files - Output of examples: "Hello by PHP print Hello by PHP print()", "Hello by PHP print this is multi line text printed by PHP print statement", and "Hello escape "sequence" characters by PHP print"
PHP Variables
- A variable in PHP is a name of memory location that holds data
- PHP do-while loop is guaranteed to run at least once
- Syntax of PHP do-while loop:
do { //code to be executed } while (condition);
PHP Break
- The
break
statement breaks the execution of current for, while, do-while, switch, and for-each loop - If used inside an inner loop, it breaks the execution of the inner loop only
- Syntax of PHP
break
:jump statement; break;
PHP Functions
- A PHP function is a piece of code that can be reused many times
- It can take input as an argument list and return a value
- There are thousands of built-in functions in PHP
- Advantages of PHP functions: code reusability
- Examples of PHP functions:
strtolower()
,strtoupper()
,ucfirst()
,lcfirst()
,ucwords()
, andstrrev()
PHP String Functions
-
strtolower()
function returns a string in lowercase letters -
strtoupper()
function returns a string in uppercase letters -
ucfirst()
function returns a string converting the first character into uppercase -
lcfirst()
function returns a string converting the first character into lowercase -
ucwords()
function returns a string converting the first character of each word into uppercase -
strrev()
function returns a reversed string
PHP Math
- PHP provides many predefined math constants and functions
- Math functions:
abs()
,ceil()
,floor()
,sqrt()
,decbin()
, anddechex()
-
abs()
function returns the absolute value of a given number -
ceil()
function rounds fractions up -
floor()
function rounds fractions down -
sqrt()
function returns the square root of a given argument -
decbin()
function converts a decimal number into binary -
dechex()
function converts a decimal number into hexadecimal
PHP Data Types
- PHP supports 8 primitive data types, categorized into 3 types: Scalar, Compound, and Special.
- Scalar types: 4 types - boolean, integer, float, and string.
- Compound types: 2 types - array and object.
- Special types: 2 types - resource and NULL.
PHP Operators
- PHP operators are symbols used to perform operations on operands.
- Categorized into 10 forms: Arithmetic, Comparison, Bitwise, Logical, String, Incrementing/Decrementing, Array, Type, Execution, and Error Control.
- Operators can also be categorized into 3 forms based on the number of operands: Unary, Binary, and Ternary.
PHP Comments
- PHP comments are used to describe code or hide code.
- Supports single line and multi-line comments.
- Single line comments: 2 ways to use -
//
(C++ style) and#
(Unix Shell style). - Multi-line comments: enclose all lines within
/* */
.
PHP Control Statements
- PHP if-else statement: used to test conditions, with various forms - if, if-else, if-else-if, and nested if.
- PHP switch statement: used to execute one statement from multiple conditions, similar to if-else-if statement.
PHP Arrays
- Associative array: a type of array that uses strings as keys.
- Two ways to define associative array: using the
array()
function or by assigning each key-value pair separately. - PHP Multidimensional array: an array of arrays, used to store tabular data.
PHP Math
- PHP provides many predefined math constants and functions.
- Math functions:
abs()
,ceil()
,floor()
,sqrt()
,decbin()
, anddechex()
.
PHP Include File
- PHP allows you to include files to reuse page content.
- Two ways to include files:
include
andrequire
. - Advantage: code reusability.
-
include
vsrequire
:include
allows the script to continue if the file is missing, whilerequire
halts the script with a fatal error.
Learn about PHP comments and control statements, including single-line and multi-line comments, if-else statements, and more.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free