Assignment questions Unit-3
1. Explain Variable Scope in PHP. Discuss the types of Variable Scope
with examples.(Refer Model papers)
2. Explain the different conditional statements in PHP
3. Explain Looping statements in PHP
4. Explain the any difference between echo and print command
5. explain PHP comments with example
6. Explain the Looping statements with an examples
7.exaplain operators with an example.
8.Explain Operator Precedence and Associativity in PHP.(Task)
9What is the use of global keyword in php.
10.Explain Features of PHP .(Task).
11. Explain Multiline Commands in PHP with examples.(Task)
12.What is Variable Typing in PHP? Give an example
Ans:-The Variable Typing refers to the way in which variables are treated based on
their data types. PHP is a loosely typed language, which means that we don't have to
explicitly declare the data type of a variable before using it.
PHP automatically converts variables to the appropriate data type based on their
context.
Example:
$num = 10; //Initially , $num is an integer.
$result = $num * 3.14; //here, $num is converted to float.
13.Explain foreach Loop Statement in iterating PHP arrays with
examples.
Ans:-The foreach Loop Statement The foreach loop in PHP is a control structure
specifically designed for iterating over arrays and objects.
It provides a simple way to traverse through each element in an array or each
property of an object without the need for counters or more complex syntax.
It is particularly useful when we need to process each element of an array or
object without knowing the size or structure of the data in advance.
Syntax
foreach ($array as $value)
{
// Code to execute for each element in the array
}
Example Iterating an array
<?php
$color=Array[“red”,”yellow”,”pink”];
Foreach($color as $a)
{
Echo $a.”<br>”;
}
?>
14.Explain the different ways of variable assignment in PHP.
Variables in PHP Understanding variables in PHP is fundamental for
storing and manipulating data in a PHP program. Variables in PHP are
containers used for storing information that can be used later in the
code.
Variables are the name give to the particular memory location to store
and retrieve the data.
1. Variable Declaration or Creating Variables:
In PHP, variables are declared or created by simply using the variable
name preceded by a dollar sign (S). To create a variable in PHP, we need
to give it a name and assign a value to it using the assignment operator
(=).
Syntax:
SvariableName =value;
Example:
Sname = "Srikanth"
2.Variable Naming Rules:
Variable names in PHP must adhere to certain rules:
The rules for forming a variable name in PHP are:
It must start with a letter or the underscore character.
It cannot begin with a number.
It can only contain alpha-numeric characters and underscores (A-z, 0-
9, or).
The names are case-sensitive (Le., Scolor and SCOLOR are two
different variables).
3.Variable Assignment:
PHP does not require to declare a variable before using it. We can simply
assign a value to a variable and PHP will automatically create the
variable and assign the value to it.
We use the assignment operator (=) to assign a value to a variable.
Example: $name= "Srikanth";
$age = 40;
4.Data Type Conversion: PHP is a dynamically-typed language, which
means that the data type of a variable is determined at runtime based on
the value assigned to it. For example, if we assign a string to a variable,
and later assign an integer, PHP will handle the conversion
automatically.
Example:
$name = "Srikanth"; // This is a string variable
$name =40; // This is an integer variable
PHP variables are loosely typed, which means that they can hold
different types of data, and their type can change during the execution of
a program.
5. Variable Reuse: Once a variable is declared and assigned a value, we
can reuse it throughout the code.
We can change the value of a variable by assigning a new value to it.
Example:
<?php
$color=”yellow”
$num=10;
Echo $color;
Echo $num;
$color =”orange”;
Echo $color;
In the example above, we first declare and assign values to the variables
$color and $num. Later, we change the value of $color and echo its new
value.