Example : How to make/use comments 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 in a different way?
<!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 ----------------
<?php
echo "<script>
alert('Invalid Login ID or Password');
window.location='login.php';
</script>";
?>
----------------- 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 include/attach other pages in different ways in PHP?
In PHP, we can include other pages/files in several ways:-
-------------------------------------------
include() to attach another PHP file in current page.
<?php
include("student.php");
?>
NB: If file is missing:
- warning is shown
- program continues running
-------------------------------------------
require() Works like include, but more strict.
<?php
require("student.php");
?>
NB: If file is missing:
- fatal error occurs
= program stops immediately
-------------------------------------------
include_once() attaches file only one time.
<?php
include_once("config.php");
?>
NB: Even if written multiple times, file loads only once.
It is useful for:
- database connection
- common functions
-------------------------------------------
require_once() is same as require() but loads file only once.
<?php
require_once("db.php");
?>
NB: Most commonly used for:
- database connection
- session files
- common libraries
-------------------------------------------
Using HTML Hyperlink, we can also move from one page to another.
<a href="home.php">Home</a>
-------------------------------------------
Using Form action we can also open another PHP page after form submit.
<form action="student.php" method="post">
-------------------------------------------
Using header() which redirects page automatically.
<?php
header("Location: student.php");
?>
-------------------------------------------
Using JavaScript we can also redirects the page.
<?php
echo "<script>
window.location='student.php';
</script>";
?>
-------------------------------------------
Using iframe we can also open another page inside current page.
<iframe src="student.php" width="100%" height="500"></iframe>
-------------------------------------------
Using Navigation Menu we can also navigate the page.
<ul>
<li><a href="home.php">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
Example : How to move/transfer/navigate to another or the next PHP page from the current page on a specific event in PHP?
<?php
<a href="logout.php">Logout</a>
?>
-----------------------------------
<?php
if(!isset($_SESSION['user']))
{
header("Location: login.php");
}
?>
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 types 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 a 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 the largest/Maximum value in PHP using a PHP function?
<!DOCTYPE html>
<html>
<body>
<?php
echo(max(100,250,3,5,74,-121,532));
?>
</body>
</html>
Output :
532
Example : How to display the smallest/Minimum value in PHP using a PHP function?
<!DOCTYPE html>
<html>
<body>
<?php
echo(min(100,250,3,5,74,-121,532));
?>
</body>
</html>
Output :
-121
Example : How to display the absolute value in PHP using the PHP function?
<!DOCTYPE html>
<html>
<body>
<?php
echo(abs(-251));
?>
</body>
</html>
Output:
251
Example : How to display the square root value in PHP using the PHP function?
<!DOCTYPE html>
<html>
<body>
<?php
echo(sqrt(25));
?>
</body>
</html>
Output:
5
Example : How to Rounding Floating Point value in PHP using the 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 a random number value in PHP using a 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 the 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