Podcast
Questions and Answers
In PHP, how are variables declared?
In PHP, how are variables declared?
- With a hash sign
- With an ampersand
- With an exclamation mark
- With a dollar sign (correct)
Which data types can PHP variables hold?
Which data types can PHP variables hold?
- Integers, floats, booleans, strings, objects, and arrays (correct)
- Integers and strings only
- Letters and numbers only
- Letters only
How do you assign a value to a variable in PHP?
How do you assign a value to a variable in PHP?
- With the '==' operator
- With the '===' operator
- With the '=' operator (correct)
- With the ':=' operator
What is the correct way to initialize an indexed array in PHP?
What is the correct way to initialize an indexed array in PHP?
What is the purpose of using loops in PHP?
What is the purpose of using loops in PHP?
Which symbol is used to declare a variable in PHP?
Which symbol is used to declare a variable in PHP?
Which loop in PHP repeats until a condition becomes false?
Which loop in PHP repeats until a condition becomes false?
What is the output of the for loop provided in the text?
What is the output of the for loop provided in the text?
In PHP, how are parameters passed to functions?
In PHP, how are parameters passed to functions?
Which keyword is used to introduce a class definition in PHP?
Which keyword is used to introduce a class definition in PHP?
What does a PHP class consist of?
What does a PHP class consist of?
How do objects communicate in object-oriented programming?
How do objects communicate in object-oriented programming?
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.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
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.