Example : How to make/use Comment in different ways in Php ?
<!DOCTYPE html>
<html>
<body>
<?php
// echo "The (//) symbol is used to represent single line comment in php applied in the beginning of sentence/word";
?>
</body>
</html>
Output:
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
# echo "In some versions of Php, the hash(#) symbol is also used to represent single line comment in php";
?>
</body>
</html>
Output:
----------------- OR ------------------
<!DOCTYPE html>
<html>
<body>
<?php
/* The symbol(/*...*/) is used to represent multiline comment in php applied in the beginning and end of the sentence */
?>
</body>
</html>
Output:
Example : How to Print/Display messages in Php?
<!DOCTYPE html>
<html>
<body>
<?php echo "The echo statement is used to print the message in php."; ?>
</body>
</html>
Output :
The echo statement is used to print the message in php.
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php print "The print statement is aslo used to print the message in php"; ?>
</body>
</html>
Output:
The print statement is also used to print the message in php.
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2> Message Printing in Php</h2>";
echo "Display Message in Php1";
print "Display Message in Php2";
?>
</body>
</html>
Output:
Message Printing in Php
Display Message in Php1Display Message in Php2
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2> Message Printing in Php</h2>";
echo "Display Message in Php1 ";
print "Display Message in Php2";
?>
</body>
</html>
Output:
Message Printing in Php
Display Message in Php1 Display Message in Php2
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$msg="Message Printing in Php";
echo "<h2>". $msg . "</h2>";
echo "Display Message in Php1";
print "Display Message in Php2";
?>
</body>
</html>
Output:
Message Printing in Php
Display Message in Php1Display Message in Php2
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
echo "Display Message in Php1<br>";
ECHO "Display Message in Php2";
eCHO "<br>";
eChO "Display Message in Php3";
?>
</body>
</html>
Output:
Display Message in Php1
Display Message in Php2
Display Message in Php3
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
echo "Display Message in Php 1<br>";
ECHO "Display Message in Php 2<br>";
eChO "Display Message in Php 3";
?>
</body>
</html>
Output:
Display Message in Php 1
Display Message in Php 2
Display Message in Php 3
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$x=10;
$y=25;
$z=$x+$y;
echo $x,"<br>";
ECHO $y,"<br>";
echo $z;
?>
</body>
</html>
Output:
10
25
35
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$x=10;
$y=25;
$z=$x+$y;
echo "The addition result is = ". $z;
echo "<br>The addition result of $x and $y is = ". $z;
echo "<br>The addition result of $x + $y is = ". $z;
?>
</body>
</html>
Output:
The addition result is = 35
The addition result of 10 and 25 is = 35
The addition result of 10 + 25 is = 35
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
echo "Title of the Project is \"Online Travel Management System.\"<br>";
echo 'Title of the Project is \'Online Travel Management System.\'';
?>
</body>
</html>
Output :
Title of the Project is "Online Travel Management System."
Title of the Project is 'Online Travel Management System.'
NB: Php is not case sensitive
Example : How to use New Line output/message in Php ?
<!DOCTYPE html>
<html>
<body>
<?php
echo "Display Message in Php1<br>";
ECHO "Display Message in Php2";
eCHO "<br>";
eChO "Display Message in Php3";
?>
</body>
</html>
Output :
Display Message in Php1
Display Message in Php2
Display Message in Php3
----------------- OR ----------------
!DOCTYPE html>
<html>
<body>
<?php
echo "Welcome U all <br> In Your Site Codershelpline";
?>
</body>
</html>
Output:
Welcome U all
In Your Site Codershelpline
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
echo nl2br("Php function nl2br() along with \n and \r\n gives the new line on the browser window.");
?>
</body>
</html>
Output :
Php function nl2br() along with
and
gives the new line on the browser window.
Example : How do we declare different type of variables in Php ?
<!DOCTYPE html>
<html>
<body>
<?php
$msg1 = "Hello India!"; //String datatype.
$msg1 = 'Hello India!'; //String datatype.
$msg2 = 410; //integer datatype.
$msg3 = 3.141; //float datatype.
$msg4 = null; // null datatype.
$msg5 = true; // Boolean datatype.
$msg6 = false;
$msg7 = array("Hello","India","World"); //array datatype
?>
</body>
</html>
Example : How do we Concatenate(. Symbol) String/Variable in Php ?
<!DOCTYPE html>
<html>
<body>
<?php
$msg1 = 'Raman';
$msg2 = "Hi $msg1";
echo $msg2;
?>
</body>
</html>
Output:
Hi Raman
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$msg1 = 'Raman';
$msg2 = "Hi". $msg1;
echo $msg2;
?>
</body>
</html>
Output:
Hi Raman
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$msg1 = 'Welcome';
$msg2 = "India";
echo $msg1." ".$msg2;
$msg3=$msg1." ".$msg2;
echo "<br>";
echo $msg3;
?>
</body>
</html>
Output :
Welcome India
Welcome India
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$val = 15;
//echo "Output is =: " . $val+35; Error due to 35 is treated as non-numeric
echo "Output is = " . ($val+35);
?>
</body>
</html>
Output:
Output is = 50
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$msg1 = 'Welcome';
$msg1.= " India";
echo $msg1;
?>
</body>
</html>
Output:
Welcome India
Example : How to know the Datatype of declared Variable in Php?
<!DOCTYPE html>
<html>
<body>
<?php
$val = 3.141;
var_dump($val); // A Php Function to find out Datatype
?>
</body>
</html>
Output :
float(3.141)
----------------- OR ----------------
<!DOCTYPE html>
<html>
<body>
<?php
$val = 3141;
var_dump(is_int($val)); // Php Function to check integer Datatype
echo "<br>";
$num = 3.141;
var_dump(is_int($num));
?>
</body>
</html>
Output :
bool(true)
bool(false)
NB:
var_dump() function returns the data type and value.
is_int(), is_float()- returns int and float value true/false.
is_nan()- returns not a number true/false.
is_numeric() - returns a number is numeric,true/false.
Example : How to Typecast data in Php?
<!DOCTYPE html>
<html>
<body>
<?php
$val = 3.141;
$num= (int)$val; //convert float into int.
echo $num;
?>
</body>
</html>
Output :
3
Example : How to display Largest/Maximum value in Php using Php function ?
<!DOCTYPE html>
<html>
<body>
<?php
echo(max(100,250,3,5,74,-121,532));
?>
</body>
</html>
Output :
532
Example : How to display Smallest/Minimum value in Php using Php function ?
<!DOCTYPE html>
<html>
<body>
<?php
echo(min(100,250,3,5,74,-121,532));
?>
</body>
</html>
Output :
-121
Example : How to display Absolute value in Php using Php function ?
<!DOCTYPE html>
<html>
<body>
<?php
echo(abs(-251));
?>
</body>
</html>
Output:
251
Example : How to display Square Root value in Php using Php function ?
<!DOCTYPE html>
<html>
<body>
<?php
echo(sqrt(25));
?>
</body>
</html>
Output:
5
Example : How to Rounding Floating Point value in Php using Php function ?
<!DOCTYPE html>
<html>
<body>
<?php
echo(round(25.954));
echo "<br>";
echo(round(25.324));
?>
</body>
</html>
Output :
26
25
Example : How to generate Random Number value in Php using Php function ?
<!DOCTYPE html>
<html>
<body>
<?php
echo(rand()); // Generates any random number of 7 digit at a time.
echo(rand(23, 251)); // Generates any random number bet 23-251 at a time.
?>
</body>
</html>
Output:
2654257 .....
69 ...
Example : What is the use of Spaceship Operator(<=>) in Php ?
<!DOCTYPE html>
<html>
<body>
<?php
echo 100 <=> 100,"<br>";
echo 100 <=> 200,"<br>";
echo 200 <=> 100;
?>
</body>
</html>
Output :
0
-1
1
0 Comments