PHP Display

Display PHP Variable Values

Print or echo are used to display to the web page.

Example

<?php

echo "You are learning PHP Class in http://itcollegeall.blogspot.com";
// Output: You are learning PHP Class in http://itcollegeall.blogspot.com

echo("You are learning PHP Class in http://itcollegeall.blogspot.com");
// Output: You are learning PHP Class in http://itcollegeall.blogspot.com

print "You are learning PHP Class in http://itcollegeall.blogspot.com";
// Output: You are learning PHP Class in http://itcollegeall.blogspot.com

print("You are learning PHP Class in http://itcollegeall.blogspot.com");
// Output: You are learning PHP Class in http://itcollegeall.blogspot.com

?>

What is difference between single quotes and double quotes in PHP?

Single quotes ('') :
Will display result almost completely "as is". Or not replaced by the variable values.

Example

<?php

$country = "Saudi Arabia";

echo 'Find more information about $country.';
// Output: Find more information about $country.

?>

Output Result

Find more information about $country.




Double quotes (" ") :
Variable names are replaced by the variable values.

Example

<?php

$country = "Saudi Arabia";

echo "Find more information about $country.";
// Output: Find more information about Saudi Arabia.

?>

Output Result

Find more information about Saudi Arabia.



Display variable using echo and print

Display the value in a variable on a web page with an echo or print

Example 1.

<?php

$country = "Saudi Arabia";

echo "Find more information about $country.";
// Output: Find more information about Saudi Arabia.

?>

Output Result

Find more information about Saudi Arabia.




Example 2.

<?php

$age = 32;

print "Your age is $age.";
// Output: Your age is 32.

?>

Output Result

Your age is 32.



Display variables using echo and print


Example 1.

My name is ______, i am studying in ______ school, in class_______, i am______years old.

<?php

$name = "Waleed";
$school = "knowledge public ";
$class = "4th";
$age = 5;

echo "My name is $name, i am studying in $school school, in class $class, i am $age years old.";

?>

Output Result

My name is Waleed, i am studying in knowledge public school, in class 4th, i am 5 years old.




Example 2.

My name is ______, i am studying in ______ school, in class_______, i am______years old.

<?php

$name = "Waleed";
$school = "knowledge public ";
$class = "4th";
$age = 5;

print "My name is $name, i am studying in $school school, in class $class, i am $age years old.";

?>

Output Result

My name is Waleed, i am studying in knowledge public school, in class 4th, i am 5 years old.



Display variables within HTML

<?php

$name = "Waleed";
$schoolname = "knowledge public ";

?>

<!DOCTYPE html>

<html>
<head>
<title>Page Title</title>
</head>
<body>

<p>
My name is <?php echo $name ?>, i am studying in <?php echo $schoolname ?> school.
</p>

</body>
</html>

Output Result

My name is Waleed, i am studying in knowledge public school.



Creating a new line using Print or echo

echo "<br>"
print "<br>"

<?php

$name = "Waleed";
$school = "knowledge public ";
$class = "4th";
$age = 5;

echo "My name is $name .<br> I am studying in $school school in class $class.<br> I am $age years old.";

?>

Output Result

My name is Waleed .
I am studying in knowledge public school in class 4th.
I am 5 years old.



Share This:

No comments:

Post a Comment

Advertiser
Counter
Flag Counter