24 Questions
What is the purpose of the if...else statement?
To execute different blocks of code based on multiple conditions
What is the syntax for the if statement?
if (condition) { code to be executed if condition is true; }
What is the purpose of the switch statement?
To select one of many blocks of code to be executed
What is the effect of the expression $a += 5?
It increments the value of $a by 5
What is the purpose of the if...else if....else statement?
To execute different blocks of code based on multiple conditions
In PHP, what is the purpose of the $ symbol in a variable name?
To denote a variable
What is the output of the following code: <?php $t=date("H"); if ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
Have a good day!
What happens when you add a double to an integer in PHP?
The result will always be a double
What is the purpose of arithmetic operations in PHP?
To perform mathematical operations on variables
What is the purpose of the is_double() function in PHP?
To check if a variable is a double
What is the purpose of concatenation in PHP?
To join strings into one
What is the use of the \n escape sequence in PHP?
To insert a new line character
In PHP, what happens when you try to use a string in an arithmetic expression?
The string is converted to a number up to the first non-numeric character
What is the purpose of type casting in PHP?
To temporarily change the data type of a variable
What is the use of the is_array() function in PHP?
To check if a variable is an array
What is the purpose of the \t escape sequence in PHP?
To insert a horizontal tab
What is the primary use of PHP in web development?
To generate dynamic web pages
How are PHP scripts executed?
On the server-side by the web server
What is the default file extension for PHP files?
.php
What happens when a PHP script is accessed by a client's web browser?
The PHP code is executed on the server-side and the output is sent to the browser
How are string literals defined in PHP?
Using single quotes and double quotes
What is the purpose of variable interpolation in PHP?
To replace variables with their values
What is the similarity between PHP and C/C++?
Syntax and paradigm
What is the purpose of the reserved PHP tag?
To start a PHP script
Study Notes
Arithmetic Operations
- Subtraction:
$a - $b
- Multiplication:
$a * $b
- Division:
$a / $b
- Assignment operators:
$a += 5
(also works for*=
and/=
)
Concatenation
- Use a period to join strings into one
PHP Control Structures
- Control structures: allow us to control the flow of execution through a program or script
- Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops)
PHP Conditional Statements
- if statement: executes some code only if a specified condition is true
- if...else statement: executes some code if a condition is true and another code if the condition is false
- if...else if....else statement: selects one of several blocks of code to be executed
- switch statement: selects one of many blocks of code to be executed
The if Statement
- Syntax:
if (condition) { code to be executed if condition is true; }
- Example: output "Have a good day!" if the current time is less than 20
The if...else Statement
- Syntax:
if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
- Example: output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise
PHP Details
- Procedural language
- C-like syntax:
{ } ;
- Extensive Function Library
- Good Web-server integration
- Script embedded in HTML
- Easy access to form data and output of HTML pages
- Not fully object-oriented
PHP and HTML
- HTML-embedded
- PHP scripts are essentially HTML pages with the occasional section of PHP script
- PHP script is enclosed in the tag pair:
<?php ?>
Variables
- Creation of variables: variable names always start with
$
sign - Rest of the variable name can contain alphanumeric, underscore, and hyphen
- Example:
$name = “abc”;
,$age = 36;
- PHP is case sensitive
Escape Sequences in PHP
-
\n
- Insert a new line character -
\r
- Carriage return -
\t
- Horizontal tab -
\\
- Backslash -
\$
- Dollar -
\"
- Double quote
Changing Data Type
- Type casting process involves dynamically changing the type of data item
- If we add a double to an integer, the result will be a double even if we are storing it in the original integer variable
Type Checking
-
is_array(var)
- returns TRUE if the variable is an array -
is_double(var)
- returns TRUE if the variable is a double -
is_float(var)
- returns TRUE if the variable is a floating point number -
is_int(var)
- returns TRUE if the variable is an integer -
is_string(var)
- returns TRUE if the variable is a string -
is_object(var)
- returns TRUE if the variable is an object
What is PHP?
- "PHP: Hypertext Preprocessor"
- Open-source, server-side scripting language
- Used to generate dynamic web-pages
- PHP scripts reside between reserved PHP tags
- Interpreted language, scripts are parsed at run-time rather than compiled beforehand
- Executed on the server-side
- Source-code not visible by client
What Can PHP Do?
- Generate dynamic page content
- Create, open, read, write, delete, and close files on the server
- Collect form data
- Send and receive cookies
- Add, delete, modify data in your database
- Control user-access
- Encrypt data
What does PHP code look like?
- Structurally similar to C/C++
- Supports procedural and object-oriented paradigm (to some degree)
- All PHP statements end with a semi-colon
- Each PHP script must be enclosed in the reserved PHP tag
Comments in PHP
- Standard C, C++, and shell comment symbols
-
//
C++ and Java-style comment -
#
Shell-style comments
String Handling
- String literals (constants) enclosed in double quotes “ ” or single quotes ‘ ’
- Within “”, variables are replaced by their value: – called variable interpolation
Learn about basic PHP operations and control structures, including arithmetic operations, concatenation, and conditional statements.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free