Bundle: PHP

Progress (0%)

PHP Foundation - Questions

Pack 1 97 questions

Things to know

  1. PHP Foundation unit required

Introduction

  1. What does 'PHP' stand for?

    PHP is a recursive acronym for PHP: Hypertext Preprocessor. Learn more in Introduction to PHP.

  2. Is PHP a frontend technology or a backend technology?

    PHP is a backend technology that helps us in building the server-side logic of web applications. Learn more in Introduction to PHP.

  3. Who created PHP, initially?

    Rasmus Lerdorf is the one who created the PHP language. Back then, when he released its very first version in mid 1995, it was completely different from the PHP that we all know of and use today. Learn more in Introduction to PHP.

  4. In which year was PHP first released?

    In the year 1995. Learn more in Introduction to PHP.

  5. What does it mean when we say that PHP is a scripting language?

    PHP being a 'scripting' language means that it runs inside a virtual environment, also known as an engine or implementation, that is simply a program coded in another computer language that is capable of parsing and executing PHP code. Learn more in Introduction to PHP.

  6. What does it mean when we say that PHP is a high-level language?

    PHP being a 'high-level' language means that it abstracts away many of the complexities of programming a computer machine and, instead, provides us with a very simple and easy-to-understand English-like syntax to code in. Learn more in Introduction to PHP.

  7. What is meant by an implementation of PHP?

    An implementation of PHP simply refers to a computer program that is capable of parsing and converting PHP code into another format or executing it right after the parsing. Learn more in Introduction to PHP.

  8. Name the most popular implementation of PHP.

    Zend Engine, written in C. Learn more in Introduction to PHP.

  9. Name a couple other implementations of PHP.

    HHVM, PeachPie, Parrot. Learn more in Introduction to PHP.

  10. Name at least 2 programming paradigms supported by PHP.

    Procedural, object-oriented and functional. Learn more in Introduction to PHP.

First Program

  1. What is the extension of a PHP file?

    .php is the extension of a PHP file. Learn more in PHP First Program.

  2. Is it necessary for a file to have the correct extension for the PHP engine to be able to execute it?

    No. If we have a file called program.txt that contains PHP code and then try executing it, the PHP engine won't complain at all. Learn more in PHP First Program.

  3. What is the significance of <?php and ?> in a PHP file?

    They denote PHP code in a PHP file. Learn more in PHP First Program.

  4. What is the echo keyword?

    The echo keyword is a means of outputting text to the standard output stream in PHP. Learn more in PHP First Program.

  5. What is a string?

    A string is a sequence of characters that denotes literal text in a program. Learn more in PHP First Program.

  6. Give one way to denote a string in PHP.

    Using a pair of single quotes (''). Learn more in PHP First Program.

  7. What is the php.exe file that comes with the installation of PHP and what is its purpose?

    php.exe is an executable program that is meant to parse and execute PHP code. Sometimes it's also referred to as the PHP interpreter, or the PHP engine. Learn more in PHP First Program.

Basics

  1. What is a string?

    A string is a sequence of characters that denotes literal text in a program. Learn more in PHP Basics — Strings.

  2. Give two ways to denote a string in PHP.

    Using a pair of single quotes ('') and a pair of double quotes (""). Learn more in PHP Basics — Strings.

  3. What's the difference between single-quoted and double-quoted strings in PHP?

    We can't have escape sequences inside single-quoted strings besides \' (to denote ' characters) and \\ (to denote \ characters) — everything inside a single-quoted string is, more or less, parsed literally. In contrast, in double-quoted strings, we can have a wide variety of escape sequences and they also support interpolation. Learn more in PHP Basics — Strings.

  4. Can a single-quoted or double-quoted string in PHP span multiple lines in source code?

    Yes. It's perfectly valid to do so.

    <?php
    
    $str = 'Single-quoted strings in PHP
    can span
    multiple lines';
    <?php
    
    $str = "Double-quoted strings in PHP
    can also span
    multiple lines";

    Learn more in PHP Basics — Strings.

  5. What is an escape sequence?

    An esacpe sequence denotes a special character in a string. It begins with the backslash (\) symbol and is followed by a character to, as a whole, represent the special character. An example is \n, used to denote a newline character. Learn more in PHP Basics — Strings.

  6. List any three escape sequences.

    Here are some escape sequences: \n, \t, \r, \\, \', \". Learn more in PHP Basics — Strings.

  7. What does \n denote in the string "Hello\nWorld!"?

    A newline character. Learn more in PHP Basics — What are escape sequences?

  8. What is an operator?

    An operator is basically a symbol or a keyword that performs an operation over one or more given values. An example is the arithmetic addition operator, denoted as +, that adds two numbers together. Learn more in PHP Basics — What is an operator?

  9. What is string concatenation?

    String concatenation refers to joining together two strings into one single string. Learn more in PHP Basics — Strings.

  10. Which symbol is used to perform string concatenation in PHP?

    The period (.) symbol. Learn more in PHP Basics — Strings.

  11. What are integers?

    Integers are whole numbers, i.e. numbers without a fractional part. Examples are 1, 2, 3, 1000, -5, -50, etc.

  12. What are floats?

    Floats, or floating-point numbers, are numbers with a fractional part. Examples include 1.0 (the fractional part is zero but there is one and so the number is a float), 2.45, 3.00000001, 1000.5, -5.111, -50.2478, etc.

  13. PHP has separate data types for integers and floats. True or false? If false, then give the correct explanation.

    Yes, this is true. Learn more in PHP Basics — Numbers.

  14. Name any five basic arithmetic operators that PHP provides along with their symbols.

    Here are all the the six arithmetic operators of PHP: addition (+), subtraction (-), multiplication (*), division (/), exponentiation (**) and modulo (%). Learn more in PHP Basics — Basic arithmetic.

  15. What is meant when we say that the multiplication operator has a higher precedence than the addition operator in PHP?

    It means that when both the multiplication (*) and the addition (+) operators exist in an operation, without any parentheses (()), multiplication would be performed first. For example, in the expression 2 * 5 + 10, multiplication would happen first and then addition, giving the result 20 (instead of 30 if addition was done first). Learn more in PHP Basics — Basic arithmetic.

  16. How can we enforce a particular operation to be performed before the other in an expression in PHP?

    By grouping it in a pair of parentheses (()). Learn more in PHP Basics — Basic arithmetic.

  17. What is a variable?

    There are multiple ways of defining a variable. For example:

    • A variable is a container for holding data.
    • A variable is a name in a program, referring to a location in memory where data is stored.

    Learn more in PHP Basics — Variables.

  18. How to create a variable in PHP?

    We begin with the $ symbol, followed by the name of the variable to create, followed by the = symbol, followed by the value to initialize the variable with. Learn more in PHP Basics — Variables.

  19. Variables in PHP are case-sensitive. What does this mean?

    It means that two variables with names having different cases are considered to be different. That is, $foo is different from $FOO, $Foo, $fOO, etc. Learn more in PHP Basics — Variables.

  20. What is a semantic error?

    A semantic error is an error in the meaning of a program. The program might run to completion but not produce the correct output. Learn more in PHP Basics — What is a semantic error?

  21. What is meant by 'standard input'?

    Standard input refers to the stream that is pointed to by the STDIN constant provided by PHP. Typically, standard input in a PHP CLI program is tied to input from the keyboard. Learn more in PHP Basics — Standard input.

  22. What is a function in PHP?

    A function represents a block of code that is executed whenever the function is called. Learn more in PHP Basics — What is a function?

  23. What is the fgets() function meant for?

    fgets() is meant to read data from a given file. Learn more in PHP Basics — Standard input.

  24. What is a constant?

    A constant behaves more or less like a variable, except for that once assigned a value, it can't be changed and that it doesn't begin with the $ character. Learn more in PHP Basics — What is a constant?

  25. What is STDIN?

    STDIN is a constant that points to the standard input stream in PHP. Learn more in PHP Basics — Standard input.

  26. What is a prompt message?

    A prompt message, which is immediately followed by an input request, describes what is required to be input (and even gives a visual hint for this using characters such as > or :).

    In the following code, the echo statement outputs a prompt message:

    <?php
    
    echo 'Enter your name> ';
    $input = fgets(STDIN);

    Learn more in PHP Basics — Standard input.

  27. How to convert a string to an integer in PHP using a typecast?

    (int) string_value. Learn more in PHP Basics — Converting to an integer.

  28. What is a statement?

  29. What is an expression?

    An expression is a unitary piece of code that resolves down to a single value. An expression can be composed of expressions itself. Learn more in PHP Basics — Statements and expressions.

  30. What is a keyword?

    A keyword is simply a word that has a special meaning associated with it. An example is echo. It doesn't represent a constant value in PHP; instead, it serves to output text to the standard output stream. Learn more in PHP Basics — Statements and expressions.

  31. What is an expression statement?

    An expression statement is a statement that is merely an expression. If PHP expects a statement somewhere and we provide it with an expression, what we have now is an expression statement. Learn more in PHP Basics — Statements and expressions.

  32. Any expression can be used in a place where PHP expects a statement. True or false?

    True. All expressions can act as statements. Learn more in PHP Basics — Statements and expressions.

  33. Any statement can be used in a place where PHP expects an expression. True or false?

  34. What is a syntax error?

    A syntax error is an error in the syntax of code. Learn more in PHP Basics — What is a syntax error?

Variables

  1. Give two rules for naming variables in PHP.

    Here are the rules for naming variables in PHP:

    • Names can't begin with a digit. Hence, $2nd is wrong.
    • Names can only contain alphanumeric characters (a-z, A-Z, 0-9) and the _ (underscore) character; nothing else.
    • Names can't contain spaces. Hence, $first word is wrong.

    As a side note, surprisingly, for people having experience of another programming language, variables in PHP can be named after keywords. For instance, the variable $for is perfectly valid in PHP even though there is a for keyword in the language.

    Learn more in PHP Basics — Variables — Rules for naming variables.

  2. Which casing convention is mostly used by PHP for its built-in functions' names?

    For a long time, PHP has sticked to using the snake case convention, i.e. snake_case, for naming built-in functions. Learn more in PHP Basics — Variables.

  3. PHP is a dynamically-typed language. What does this mean?

    This simply means that variables in PHP don't have fixed (static) types; instead, the data type of the value stored in a variable can change during the course of the program. Hence, we say that PHP is a dynamically-typed language. Learn more in PHP Basics — Variables — Dynamically-typed nature of PHP.

  4. What is a variable variable in PHP?

    A 'variable variable' in PHP is a variable whose name is obtained using another variable. Learn more in PHP Basics — Variables — Variable variables.

  5. Give an example of a variable variable.

    Supposing the $name is a variable holding the value 'greeting' and $greeting is another variable holding the value 'Hello'. Then $$name is a variable variable which simply becomes $greeting and, thus, resolves down to 'Hello'. Learn more in PHP Basics — Variables — Variable variables.

  6. How to determine whether a particular variable exists in PHP?

    Using the isset() function. Learn more in PHP Basics — Variables — Determining a variable's existence.

  7. Specify an issue with using isset() to determine a variable's existence.

    It returns false even for variables that exist albeit have their value as NULL. Learn more in PHP Basics — Variables — isset() checks for NULL.

  8. What is the var_dump() function in PHP used for?

    var_dump() is used to print a value in PHP along with its type information. Learn more in PHP Basics — Variables — var_dump().

Comments

  1. What are comments in PHP?

    Comments are statements in a program that are ignored, i.e. they just don't get executed. Learn more in PHP Comments.

  2. Give two uses of comments in PHP.

    Let's give three uses of comments in programs: to explain code; to document code; and to test/debug given code. Learn more in PHP Comments.

  3. Name the two kinds of comments in PHP.

    Single-line comments and multi-line comments. Learn more in PHP Comments — How to comment?

  4. How to write a comment of each kind?

    Single-line comments are denoted using either # or //, whereas multi-line comments are denoted using /* */. Learn more in PHP Comments — How to comment?

  5. Give two tips for writing good comments.

Data Types

  1. What is a data type?

    A data type is a set of values along with the operations possible on those values. Learn more in PHP Data Types.

  2. Give the three classifications of data types in PHP.

    We have the following three classifications: scalar, compound, and special. Learn more in PHP Data Types — Classifications of data types.

  3. For each classification, give all of the data types that it contains.

    We have 4 scalar data types: integers, floats, Booleans, and strings; 4 compound data types: arrays, objects, callables, and iterables; and 2 special data types: resources and NULL. Learn more in PHP Data Types — Classifications of data types.

  4. In PHP, the numbers 10 and 10.5 are considered to be of different data types. True or false?

    Absolutely true. PHP has the customary two types for representing numbers in programming: integers and floats. 10 is an integer whereas 10.5 is a float. Learn more in PHP Data Types — Integers and PHP Data Types — Floats.

  5. In PHP, the numbers 10 and 10.0 are considered to be of different data types. True or false?

    Absolutely true. Even though mathematically 10 and 10.0 are the same, with the presence of .0 in 10.0, it becomes a float, whereas 10 is an integer. Learn more in PHP Data Types — Integers and PHP Data Types — Floats.

  6. How to convert an arbitrary value $v to an integer using a typecast?

    Using the (int) typecast as follows: (int) $v. Learn more in PHP Data Types — Integers.

  7. How to determine if an arbitrary value $v in PHP is an integer?

    Using the is_int() function, providing the arbitrary value to it as an argument. Learn more in PHP Data Types — Integers.

  8. How to convert an arbitrary value $v to a float using a typecast?

    Using the (float) typecast as follows: (float) $v. Learn more in PHP Data Types — Floats.

  9. How to determine if an arbitrary value $v in PHP is a float?

    Using the is_float() function. Learn more in PHP Data Types — Floats.

  10. What is meant by the length of a string?

    It refers to the total number of characters in the string. Learn more in PHP Data Types — Strings.

  11. How to determine the length of a string in PHP?

    Using the strlen() function. Learn more in PHP Data Types — Strings.

  12. What is meant by the index of a character in a string?

    The index of a character simply refers to its position in the string. Indexes in PHP start at 0. Learn more in PHP Data Types — Strings.

  13. How to retrieve a given character from a string in PHP?

    Using bracket notation following the string value. Learn more in PHP Data Types — Strings.

  14. How to retrieve the second character of a string $str in PHP?

    Using the expression $str[1] as the second character's index is 1. Learn more in PHP Data Types — Strings.

  15. Give two ways to convert an arbitrary value $v in PHP to a string?

    We can either use the (string) typecast, as in (string) $v, or concatenate the value with an empty string, as in '' . $v. (There's even a third way and that's to use the strval() function). Learn more in PHP Data Types — Strings.

  16. How to determine if an arbitrary value $v in PHP is a string?

    Using the is_string() function. Learn more in PHP Data Types — Strings.

  17. What is a Boolean?

    A Boolean is a true or false value. Learn more in PHP Data Types — Booleans.

  18. How to convert an arbitrary value $v in PHP to a Boolean?

    Using the typecast (bool), as in (bool) $v. (We can also use the boolval() function.)

  19. How to determine if an arbitrary value $v in PHP is a Boolean?

    Using the is_bool() function. Learn more in PHP Data Types — Booleans.

  20. What is an array?

    An array is an ordered sequence of elements. Learn more in PHP Data Types — Arrays.

  21. What is meant by an 'element' of an array?

    An element of an array simply refers to one of the items stored in it. Learn more in PHP Data Types — Arrays.

  22. What is meant by the index of an element of an array?

    An element's index refers to its position in the array.

  23. What is the total number of items in an array called?

    It's called the array's length.

  24. What is an empty array and how to denote one in PHP?

    It's an array without any elements. An empty array in PHP is denoted as []. Learn more in PHP Data Types — Arrays.

  25. What is meant by an array literal in PHP?

    An array literal in PHP is the direct — the 'literal' — representation of an array in source code. It's denoted using a pair of square brackets ([]). Learn more in PHP Data Types — Arrays.

  26. Which function can be used to add elements to the end of an array after it has been defined?

    The array_push() function can be used to add elements to the end of an array. Learn more in PHP Data Types — Arrays.

  27. What happens when we try to echo an array value and why?

    We get a warning because it's not meaningful to convert an array to a string in PHP. Learn more in PHP Data Types — Arrays.

  28. How to print a readable representation of an array in PHP?

    Using the print_r() function.

  29. How to sort an array in PHP?

    Using the sort() function.

  30. What means when we say that a function modifies an array 'in-place'?

    It means that the function modifies the original array. Learn more in PHP Data Types — Arrays.

  31. How to determine if an arbitrary value $v in PHP is an array?

    Using the is_array() function.

  32. What does the gettype() function do?

    The gettype() function is used to obtain the type of a given value in PHP, as a string. For example, gettype(10) returns the string 'integer'. Learn more in PHP Data Types — gettype().

  33. Supposing that $v holds a float, gettype($v) returns the string 'float'. True or false? Explain your answer.

    False. gettype($v), where $v is a float, returns the string 'double'. This is for backwards-compatibility with older versions of PHP. Learn more in PHP Data Types — gettype().