0

Basic PHP Quiz 2

Description: Basic PHP Quiz 2
Number of Questions: 10
Created by:
Tags: php
Attempted 0/10 Correct 0 Score 0

What is the difference between print() and echo()?

php
  1. print() can be used as part of an expression, while echo() can't

  2. echo() can be used as part of an expression, while print() can't

  3. echo() can be used in the CLI version of PHP, while print() can't

  4. print() can be used in the CLI version of PHP, while echo() can't


Correct Option: A

AI Explanation

To answer this question, we need to understand the difference between the print() and echo() functions in PHP.

Option A) print() can be used as part of an expression, while echo() can't This option is correct. The print() function can be used as part of an expression, meaning it can be used within another statement or assignment. For example, you can use print($x + $y) to display the result of the addition of $x and $y. On the other hand, the echo() function cannot be used as part of an expression.

Option B) echo() can be used as part of an expression, while print() can't This option is incorrect. As mentioned above, echo() cannot be used as part of an expression.

Option C) echo() can be used in the CLI version of PHP, while print() can't This option is incorrect. Both print() and echo() can be used in the CLI (Command Line Interface) version of PHP.

Option D) print() can be used in the CLI version of PHP, while echo() can't This option is incorrect. Both print() and echo() can be used in the CLI version of PHP.

Therefore, the correct answer is A) print() can be used as part of an expression, while echo() can't.

What is displayed when the following script is executed?

define(myvalue, "10");
$myarray[10] = "Dog";
$myarray[] = "Human";
$myarray['myvalue'] = "Cat";
$myarray["Dog"] = "Cat";
print "The value is: ";
print $myarray[myvalue]."\\n";
php
  1. The value is: Dog

  2. The value is: Cat

  3. The value is: Human

  4. The value is: 10


Correct Option: A

Which of the following is not valid PHP code?

php
  1. $_10

  2. ${"MyVar"}

  3. &$something

  4. $10_somethings


Correct Option: D

AI Explanation

To answer this question, let's go through each option and determine if it is valid PHP code:

Option A) $_10 - This option is valid PHP code. Variable names in PHP can start with a letter or an underscore, followed by any combination of letters, numbers, or underscores.

Option B) ${"MyVar"} - This option is valid PHP code. It demonstrates the use of variable variables in PHP, where the variable name is constructed by using the value of another variable.

Option C) &$something - This option is valid PHP code. It demonstrates the use of a reference variable in PHP, denoted by the ampersand symbol (&).

Option D) $10_somethings - This option is not valid PHP code. Variable names in PHP cannot start with a number. They must start with a letter or an underscore.

Therefore, the correct answer is D) $10_somethings. This option is not valid PHP code because it violates the naming conventions for variables in PHP.

Which of the following tags is not a valid way to begin and end a PHP code block?

php
  1. = ?>


Correct Option: D

When turned on, __________ will __________ your script with different variables from HTML forms and cookies.

php
  1. show_errors, enable

  2. show_errors, show

  3. register_globals, enhance

  4. register_globals, inject


Correct Option: D

What is the value of $result in the following PHP code?

function timesTwo($int)
{
  $int = $int * 2;
}

$int = 2;

$result = timesTwo($int);
php
  1. 0

  2. 2

  3. 4

  4. NULL


Correct Option: D

Which of the following prints ‘text/xml’?

php
  1. print substr($text, strchr($text, ':'));

  2. print substr($text, strchr($text, ':') 1);

  3. print substr($text, strpos($text, ':') 1);

  4. print substr($text, strpos($text, ':') 2);


Correct Option: D

What does echo count (“123?) print out?

php
  1. 3

  2. false

  3. null

  4. 1


Correct Option: D

Which of the following snippets prints a representation of 42 with two decimal places?

php
  1. printf("%.2d\n", 42);

  2. printf("%1.2f\n", 42);

  3. printf("%1.2u\n", 42);

  4. none of the above


Correct Option: B

Which of the following expressions multiply the value of the integer variable $a by 4?

php
  1. $a *= pow (2, 2);

  2. $a >>= 2;

  3. $a <<= 2;

  4. $a = $a $a;


Correct Option: A,C
- Hide questions