Monday, 11 May 2020

Chapter 2: Introduction to PHP Programming and PHP Variables (Questions and Answers)(#ipumusings)(#PHPTutorials)

Chapter 2: Introduction to PHP Programming and PHP Variables 

(Questions and Answers)

Chapter 2: Introduction to PHP Programming and PHP Variables (Questions and Answers)(#ipumusings)(#PHPTutorials)


Q1: What are the two ways PHP can be used?

Answer:
   1. By using PHP templating engine which allows PHP code to output its content alongside HTML content.
   2. Use PHP in the Interactive Shell which is the most common way PHP scripts are executed on a server.


Q2: Give an example to show PHP is used in interactive mode.

Answer:
1. Open command shell (in Windows) or Bash shell in Linux.
2. Go to bin folder where PHP is installed.
3. Run php -a. It opens PHP as an interactive shell. php> command prompt appears.
4. Type echo "Hello World". It displays the string.
5. Type exit to quit interactive shell.


Q3: Give an example to run a PHP script at the command prompt?



Answer: Follow these steps
1. Open notepad or any other text editor and type the following.
     <?php
      echo "Hello World";
      exit;
      ?>

 2. Save the file as "helloworld.php".

 3. Open command prompt or shell and run command php helloworld.php. (Make sure php.exe is in PATH or type the full path.)


 Q4: Check what version of PHP is running on your server?

Answer: Save file checkver.php with following code:
 <?php echo phpversion(); ?>

 Run it at the command prompt or open page in a browser.


Q5: What are the variables in PHP?

Answer: Variables are containers that store some values or information. A variable starts with '$'
e.g.
$weburl = "eduvictors.com";
$x = 5;
$found = True;
$salary = 240.56;

$weburl stores a string (or text) type data. While $x stores an integer value 5. $found is a boolean type and $salary stores a floating number.


Q6: Which statement is used to display the value of a variable on a screen? Give an example.

Answer:  echo statement or print.
e.g.
$txt = "Welcome to PHP world!";
echo $txt;
print $txt;

(Note: There is a small difference between print and echo. The print statement returns value while echo statement does not.)


Q7: Are variable names case sensitive?

Answer: Yes. $txt, $TXT, $Txt are three different variables.


Q8: What are the rules for naming a variable?

Answer: Rules for PHP variables are:
A variable starts with the $ (dollar) sign, followed by the name of the variable e.g. $name

A variable name can have a combination of letters, numbers and underscore but it must start with a letter or the underscore character. e.g. $my_profile

A variable name cannot start with a number.

A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

Variable names are case-sensitive ($txt, $TXT, $Txt are three different variables.)


Q9: Are integer variables by default signed or unsigned?

Answer: Signed values.


Q10: Write ways to store integers as decimal, binary, octal and hexadecimal numbers.

Answer: Here is the snippet:
<?php
echo 'Types of integer variables';
$var1 = 1234; // decimal number
$var2 = 0b11; // binary number (2 decimal)
$var3 = 0126; // octal number (83 decimal)
$var4 = 0x1A; // hexadecimal number (26 decimal)
echo '<pre>';
echo $var1."\n";
echo $var2."\n";
echo $var3."\n";
echo $var4."\n";
echo '</pre>';
?>

Output is:

Chapter 2: Introduction to PHP Programming and PHP Variables (Questions and Answers)(#ipumusings)(#PHPTutorials)


Q11: Give an example to store floats.

Answer:
$floatvar1 = 1.234;
$floatvar2 = 5e2;

Note, floatvar2 stores 5 × 10²


Q13: How can we initialize a variable with no value?

Answer: Using the null assignment.
$var = null;


Q14: What is the use of the var_dump( ) function?

Answer: The PHP var_dump() function returns the data type and value.
e.g.
$var1 = 10.52;
var_dump($var1);

var_dump will output float(10.52).


Q15: What is an array variable in PHP?

Answer: An array is single variable that stores multiple values.

e.g. $colorName = array('red', 'yellow', 'blue', 'green');


Q16: What is the difference between the following two variables?
$var1 = 10;
$var2 = "10";

Answer: $var1 stores an intger value while $var2 stores a string value.


Q17:  What are object variables?

Answer: PHP supports object-orientation. One can define classes and objects. A class is a collection of variables and functions together.

An object or object variable is an instance of a class used to store values in the class variables and access them via the functions.

e.g.
$objVar = new stdClass();

The above statement creates an object of class stdClass, which a predefined, empty class.

No comments:

Post a Comment