29 October

class 3 :PHP is a Loosely Typed Language,PHP If & Else Statements

PHP is a Loosely Typed Language

  • In PHP a variable does not need to be declared before being set.
  • In the example above, you see that you do not have to tell PHP which data type the variable is.
  • PHP automatically converts the variable to the correct data type, depending on how they are set.
  • In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
  • In PHP the variable is declared automatically when you useit.

PHP supports eight primitive types :
Four scalar types:
boolean
integer
Float (floating-point number, aka'double')
string
Two compound types:
array
object

And finally two special types:
resource
NULL

PHP Operators

c = a + b

Assuming a and b are numbers (remember data types?
the result would be different if they were strings  more
about that later) this expression would add a and b and put
the sum into c (remember again, that the equals sign here
makes this an assignment instruction.)
In the expression a
makes this an assignment instruction.)
In the expression a + b the plus sign is an arithmetic operator. Here are some
more operators:

  • Arithmetic Operators:
  • Comparison Operators:
  • Logical Operators:
  • Increment and Decrement Operators:
  • The Concatenate Operato

PHP If & Else Statements

The if, elseif and else statements in PHP are used to perform different actionsbased on different conditions.

Conditional Statements

Very often when you write code, you want to perform different actions for differentdecisions.

You can use conditional statements in your code todo this.

if...else statement
-
use this statement if you want toexecute a set of code when a condition is true and another

if...else statement
-
use this statement if you want toexecute a set of code when a condition is true and another if the condition is not true

elseif statement -is used with the if...else statement toexecute a set of code if one of several condition are  true

Conditional Test, Events and Flows in PHP

The If...Else Statement

If you want to execute some code if a condition is
true and another code if a condition is
false, use the if....else statement.

Example

The following example will output “Name is keyul" i
f the name is keyul, otherwise it will output “Name is incorrect!":
<?
php
Conditional Test, Events and Flows in PHP
<?
php
if(isset($_REQUEST['submit']))
{
$name = $_REQUEST['name'];
if($name == "keyul")
{
echo "Name is “. $name;
}
else
{
echo "Name is incorrect";
}
}

<html>
<head>
<title> If else </title>
</head>
<body>
<form method="post">
Name : <input type="text" name="name" />
Name : <input type="text" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

 

PHP If & ElseIf Statements

If you want to execute some code if one of several
conditions are true use the elseif
statement
Syntax
if (condition)
code to be executed if condition is true;
elseif(condition)
code to be executed if condition is true;
else
code to be executed if condition is false

PHP If & ElseIf Statements
Example
The following example will output “Name is keyul" i
f the user enter value
of name sawan, and “Name is chourey!" if the user e
nter value of name
chourey, Otherwise it will output “Name is not foun
d!":
<?
php
if(isset($_REQUEST['submit']))
{
$name = $_REQUEST['name'];
$name = $_REQUEST['name'];
if($name == "sawan")
{
echo "Name is ".$name;
}
else if($name == "chourey")
{
echo "Name is ".$name;
}
else
{
echo "Name is not found";
}
}
?>

Comments


Maria Joli | 12 July 2014, 10:20

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

John Doe | 12 July 2014, 10:30

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

Jane Doe | 12 July 2014, 10:40

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

Danny Dev | 12 July 2014, 11:10

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

Post Comments