12 Questions
In PHP, how are variables declared?
With a dollar sign
Which data types can PHP variables hold?
Integers, floats, booleans, strings, objects, and arrays
How do you assign a value to a variable in PHP?
With the '=' operator
What is the correct way to initialize an indexed array in PHP?
$colors = array('red', 'green', 'blue');
What is the purpose of using loops in PHP?
To perform repetitive tasks
Which symbol is used to declare a variable in PHP?
$
Which loop in PHP repeats until a condition becomes false?
While loop
What is the output of the for loop provided in the text?
0 1 2 3 4 5 6 7 8 9
In PHP, how are parameters passed to functions?
Using parentheses
Which keyword is used to introduce a class definition in PHP?
class
What does a PHP class consist of?
Properties, methods, and constants
How do objects communicate in object-oriented programming?
By sending messages (method calls)
Study Notes
Variables in PHP
Variables in PHP are declared with the dollar sign $
prefix followed by a unique identifier. They can hold values of varying types, such as integers, floats, booleans, strings, objects, and arrays. Assigning a value to a variable involves assigning the value to the left of the equals sign (=
) to the variable name.
$myName = "John";
$myAge = 25;
You can access the stored value later with the variable name, for instance, echo $myName
.
Arrays in PHP
Arrays in PHP store collections of elements, usually separated by commas. Elements can have associated keys, allowing indexed or associative array structures. Indexed arrays use numbers starting at zero, while associative arrays use arbitrary string keys. You can initialize an array with curly braces and separate elements with commas as shown below:
$people = ["Tom", "Alice", "Dave"];
$friends = array("Steven" => 22, "Laura" => 25);
Arrays are commonly used with loop constructs like foreach, enabling iteration over array entries.
Loops in PHP
Loops are crucial for repetitive tasks. The simplest form is the for loop, which iterates a given number of times:
for ($i = 0; $i < 10; ++$i) {
echo "$i ";
}
// Output: 0 1 2 3 4 5 6 7 8 9
While loops repeat until a condition becomes false, and do..while loops repeat once regardless of truthiness:
$number = 0;
do {
echo $number;
$number++;
} while ($number != 10);
$number = 0;
while ($number != 10) {
echo $number;
$number++;
}
These commands continuously output 0
, incrementing each time through till reaching 9
.
Functions in PHP
Functions group routines together. They take input parameters and return a result. Here is a sample function that calculates the sum of two numbers:
function addTwoNumbers($n1, $n2) {
return $n1+$n2;
}
echo addTwoNumbers(25, 10); // outputs 35
To call a function, invoke the defined function name. PHP passes parameters to functions and returns values using parentheses.
Classes in PHP
Classes organize program logic into manageable units. The class keyword introduces a definition block defining properties, methods, and constants. Objects are instances of classes, inheriting characteristics and behaviors. Here is a simple class representing a person:
class Person {
public $firstName;
public $lastName;
function __construct($fn, $ln) {
$this->firstName = $fn;
$this->lastName = $ln;
}
function setFullname($fullname) {
list($f,$l) = explode (" ", $fullname);
$this->firstName = $f;
$this->lastName = $l;
}
}
$me = new Person("John","Smith");
echo $me->firstName." ". $me->lastName; // John Smith
Objects can receive messages (method calls) and send messages to other objects.
Learn about PHP essentials such as variables, arrays, loops, functions, and classes. This quiz covers topics like variable declaration, array initialization, loop constructs like for and while, function creation, and class definition for PHP programming.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free