INTRODUCTION TO PHP AND MySQL - SELF TEST 2 - USING VARIABLES AND OPERATORS

Q2-1

The PHP function to detect the type of a variable is: ______

Q 2-2

Identify which of the following variable names are invalid:

$24,

$IAMHERE,

$_error,

$^b,

${$var},

$yA_K

Q2-3

Write a PHP statement to create a constant value holding the name of your favourite ice-cream flavour.

Q 2-4

Write a PHP script to initialize a variable and then increment its value by 3.

Q 2-5

Mark the following statements as true or false:

  1. The unset() function de-initializes a variable and removes it from the program’svariable space.
  1. The PHP expressions $c = '' and $c = null are equivalent.
  1. The result of the calculation (56 - 1 * 36 % 7) is 6.
  1. The == operator tests two variables for both value and type equality.
  1. The OR logical operator has higher precedence than the AND logical operator.
  1. The is_numeric() function returns true if passed a floating-point value.
  1. Casting a floating-point number to an integer always results in the number beingrounded down.
  1. Form elements of type 'hidden' are excluded from $_POST and $_GET.

Q 2-6

What are the values of $x and ABC at the end of the following PHP script?

<?php

$x = 89;

define ('ABC', $x+1);

$x += ABC;

?>

Q 2-7

What is the likely output of the following PHP script?

<?php

$boolean = (integer) true;

$number = 1;

echo (integer) ($boolean === $number);

?>

Q 2-8

What is the likely output of the following PHP script?

<?php

define ('NUM', '7');

$a = NUM;

echo gettype($a);

?>

Q 2-9

Rewrite the code from the Dollars-to-Euros Convertor (shown below) such that both the exchange rate and the amount to be converted are supplied by the user through a web form.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""DTD/xhtml1-transitional.dtd">

<html xmlns=" xml:lang="en" lang="en">

<head>

<title> Dollars-to-Euros Convertor</title>

</head>

<body>

<h2> Dollars-to-Euros Convertor</h2>

<?php

//define exchange rate 1.00 USD = 0.70 EUR

define ('EXCHANGE_RATE', 0.70);

//define number of dollars

$dollars = 150;

//perform conversion and print result

$euros = $dollars * EXCHANGE_RATE;

echo "$dollars USD is equivalent to: $euros EUR";

?>

</body>

</html>

Q 2-10

Write a PHP script that accepts a temperature value in Celsius (C) through a webform and converts it to the Fahrenheit (F) scale. The conversion formula to use is:

F = (9/5) * C + 32.

Q 2-11

Write a PHP script to display the values entered into a web form that contains:

  1. One text input field
  2. One text area
  3. One hidden field
  4. One password field
  5. One selection list
  6. Two radio buttons
  7. Two checkboxes

A - Introduction to PHP and MySQL - Self Test 1.doc

Page 1 of 3Version 1