PHP Programming Basics
12 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • 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?

  • 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?

    <p>$colors = array('red', 'green', 'blue');</p> Signup and view all the answers

    What is the purpose of using loops in PHP?

    <p>To perform repetitive tasks</p> Signup and view all the answers

    Which symbol is used to declare a variable in PHP?

    <p>$</p> Signup and view all the answers

    Which loop in PHP repeats until a condition becomes false?

    <p>While loop</p> Signup and view all the answers

    What is the output of the for loop provided in the text?

    <p>0 1 2 3 4 5 6 7 8 9</p> Signup and view all the answers

    In PHP, how are parameters passed to functions?

    <p>Using parentheses</p> Signup and view all the answers

    Which keyword is used to introduce a class definition in PHP?

    <p>class</p> Signup and view all the answers

    What does a PHP class consist of?

    <p>Properties, methods, and constants</p> Signup and view all the answers

    How do objects communicate in object-oriented programming?

    <p>By sending messages (method calls)</p> Signup and view all the answers

    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.

    Quiz Team

    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.

    More Like This

    Use Quizgecko on...
    Browser
    Browser