0% found this document useful (0 votes)
7 views

PHP Chapter 3

PHP revision notes

Uploaded by

Ogal Peter
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PHP Chapter 3

PHP revision notes

Uploaded by

Ogal Peter
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What difference between variables and constant?

 Variables:

 They are memory locations for storing data values that can be changed or modified
during the execution of a program.
 In PHP, a variable is declared with a $ symbol followed by the variable name, like
$varName.

 Constants:

 Constants store values that cannot be changed once they are defined.
 In PHP, constants are defined using the define() function or by using the const
keyword and are case-sensitive by default.

2. Name and describe the four data type in PHP.


 String:

 Represents a sequence of characters, e.g., “Hello World”.

 Integer:

 Represents whole numbers, e.g.,. 42

 Float (also known as double):

 Represents numbers with decimal points, e.g., 3.14

 Boolean:

 Represents a value that can either be true or false.

3. Explain the purpose of the NULL data type.


NULL data type is used to refer to a variable with no value.

4. What do you mean by strongly and loosely typed programming?


 Strongly Typed:

 In strongly typed programming languages, data types are strictly enforced. You cannot
change the type of a variable once it’s declared, and type conversion must be explicit.

 Loosely Typed:
 In loosely typed languages (like PHP), variables are not bound to a specific type. PHP
automatically converts the variable type based on its value or context.

5. What is Concatenation?
This refers to joining two or more strings together using the concatenation operator . in
PHP
Practical Questions:
1. Write a simple program of the following output using Constants.
Employee code is 001
Employee Name is ABC
Employee Salary is 25000
Working Hours are 7.5
<?php
define("EMP_CODE", "001");
define("EMP_NAME", "ABC");
define("EMP_SALARY", 25000);
define("WORK_HOURS", 7.5);

echo "Employee code is " . EMP_CODE . "<br>";


echo "Employee Name is " . EMP_NAME . "<br>";
echo "Employee Salary is " . EMP_SALARY . "<br>";
echo "Working Hours are " . WORK_HOURS . "<br>";
?>
2. Repeat the above program using Variables.
Hint: Save the given data at 4 memory locations respectively and print on
the screen.
<?php
$emp_code = "001";
$emp_name = "ABC";
$emp_salary = 25000;
$work_hours = 7.5;

echo "Employee code is " . $emp_code . "<br>";


echo "Employee Name is " . $emp_name . "<br>";
echo "Employee Salary is " . $emp_salary . "<br>";
echo "Working Hours are " . $work_hours . "<br>";
?>
3. How do you make a constant name case insensitive?
<?php
define("EMP_NAME", "ABC", true);
echo EMP_NAME; // ABC
echo emp_name; // ABC
?>
4. How do you declare a constant in PHP with example?

<?php

define("GREETING", "Hello, World!");

echo GREETING;
?>
5. Write make a simple program to set your G.R No. and Name in the variables and
display G.R No. and name by echo and print functions.
<?php
$gr_no = "12345";
$name = "John Doe";

echo "G.R No: " . $gr_no . "<br>";


print "Name: " . $name;
?>
Objective & MCQ’s
1. Positive and negative number and 0 with no decimal places belong to which
data type. Integer a) Double
b) Float
c) String
d) Integer
2. Which of the following is a valid variable name? $Total-Salary;
a) TotalSalary ;
b) $Total Salary
c) $TotalSalary;
d) $Total-Salary;
3. Which is the correct syntax for declaring a variable and assigning it a string?
$Name=”Muhammad”;
a) $Name=”Muhammad”;
b) $Name=Muhammad;
c) “Muhmmad”= $Name;
d) $Name = “Muahammad”

4. How many decimal places does an integer store Integer does not store decimal places.
a) One decimal
b) Two decimal
c) Three decimal
d) Integer does not store decimal places.

5. Variable name must be starting which symbol. $


a) _ (under score)
b) @
c) $
d) %
6. Combine the two or more string or other value by using concatenation__.
(dot) _ symbol a) &
b) *
c) #
d) . (dot)
7. String constant value Must be enclosed in double quotes
a) Must be enclosed in double quotes
b) Must be enclosed in commas
c) Must be enclosed in round parentheses
d) Must be square brackets
8. A constant is case-sensitive by default. By convention, constant identifiers are
always_ Upper case __. a) Lower case
b) Camel case
c) Upper case
d) Normal case
9. We use the __ define() ______ function to create constant.
a) define()
b) Include
c) redefine
d) constant
10. A loosely typed programming language ___ Does not required data typed of a
variables to be declared. ____.
a) Does not required data typed of a variables to be declared.
b) Requires data types of variables to be declared
c) Does not have variable
d) Does not have different data types.

You might also like