All featured quizzes

PHP - Basics

PHP syntax, variables, array functions, file inclusion, and HTTP headers.

Sample Questions

A preview of what's in this quiz. Answer them interactively for instant scoring and full explanations.

What tag is used to start a PHP block in a script file?

1. What tag is used to start a PHP block in a script file?

  • A.<?php>
  • B.<?php
  • C.<script php>
  • D.<php>
Which operator is used for string concatenation in PHP?

2. Which operator is used for string concatenation in PHP?

  • A.+
  • B..
  • C.&
  • D.concat
What is the difference between `echo` and `print` in PHP?

3. What is the difference between `echo` and `print` in PHP?

  • A.print can write to files; echo can only output to the browser.
  • B.echo has no return value and is slightly faster; print returns 1.
  • C.echo is a function; print is a class constructor.
  • D.There is no difference in return value or performance.
What is an associative array in PHP?

4. What is an associative array in PHP?

  • A.An array containing database connection pools
  • B.An array that uses named string keys instead of integers
  • C.A multidimensional array containing other arrays
  • D.An array where values must be objects
What is the difference between single-quoted strings ('...') and double-quoted strings ("...") in PHP?

5. What is the difference between single-quoted strings ('...') and double-quoted strings ("...") in PHP?

  • A.Single quotes parse variables; double quotes treat text literally.
  • B.Double quotes parse variables and escape sequences; single quotes treat text literally.
  • C.Single quotes are for characters; double quotes are for strings.
  • D.There is no difference; they are identical.
What is the default index index number of the first element in a PHP indexed array?

6. What is the default index index number of the first element in a PHP indexed array?

  • A.1
  • B.0
  • C.-1
  • D.null
Which character must precede all variable names in PHP?

7. Which character must precede all variable names in PHP?

  • A.var
  • B.$
  • C.@
  • D.#
Which PHP function checks if a specific value exists inside an array?

8. Which PHP function checks if a specific value exists inside an array?

  • A.array_key_exists() (checks for keys, not values)
  • B.in_array()
  • C.contains()
  • D.has_value()
Which function is used to count the number of elements in a PHP array?

9. Which function is used to count the number of elements in a PHP array?

  • A.len()
  • B.count()
  • C.length()
  • D.size()
Which operator is used to assign key-value pairs inside a PHP associative array?

10. Which operator is used to assign key-value pairs inside a PHP associative array?

  • A.-> (used for accessing object properties/methods)
  • B.=>
  • C.:
  • D.=
Where are PHP session variables stored by default?

11. Where are PHP session variables stored by default?

  • A.In the client's browser cookies
  • B.On the web server
  • C.In local storage databases
  • D.Inside the HTML source code
Which superglobal array contains form variables sent via HTTP GET method?

12. Which superglobal array contains form variables sent via HTTP GET method?

  • A.$_POST
  • B.$_GET
  • C.$_REQUEST
  • D.$_SERVER
Which superglobal array combines data from `$_GET`, `$_POST`, and `$_COOKIE`?

13. Which superglobal array combines data from `$_GET`, `$_POST`, and `$_COOKIE`?

  • A.$_SERVER
  • B.$_REQUEST
  • C.$_ENV
  • D.$_GLOBALS
Which superglobal array contains form variables sent via HTTP POST method?

14. Which superglobal array contains form variables sent via HTTP POST method?

  • A.$_GET
  • B.$_POST
  • C.$_REQUEST
  • D.$_SESSION
How do you define a constant in PHP using the built-in function?

15. How do you define a constant in PHP using the built-in function?

  • A.const PI = 3.14; (while valid inside classes and since PHP 5.3, define() is the classic built-in function option)
  • B.define('PI', 3.14);
  • C.constant PI = 3.14;
  • D.$PI = 3.14;
Which loop structure is designed specifically for iterating over PHP arrays?

16. Which loop structure is designed specifically for iterating over PHP arrays?

  • A.for ($i=0; $i < count($array); $i++) (while valid, foreach is designed specifically for this task)
  • B.foreach ($array as $value)
  • C.while ($array)
  • D.foreach ($value in $array)
What is the purpose of the `session_start()` function?

17. What is the purpose of the `session_start()` function?

  • A.Encrypts user passwords inside databases
  • B.Starts or resumes a user session, enabling the use of $_SESSION.
  • C.Authenticates user credentials against records
  • D.Clears all cookies from the client browser
How do you access a global variable `$x` inside a PHP function?

18. How do you access a global variable `$x` inside a PHP function?

  • A.Variables are global by default in PHP.
  • B.Declare `global $x;` at the beginning of the function.
  • C.Access it using `$this->x`.
  • D.Import it using `import $x;`.
Where are PHP cookie variables stored?

19. Where are PHP cookie variables stored?

  • A.On the web server files
  • B.In the client's browser
  • C.In server-side databases
  • D.In session state caches
How do you write a string to a file in PHP using a single function?

20. How do you write a string to a file in PHP using a single function?

  • A.fwrite() (requires opening the file first)
  • B.file_put_contents()
  • C.file_write()
  • D.write_file()

Looking for a different topic? Browse all subjects