php code to display anything


To display anything in php one can use its inbuilt function 'echo' or 'print'. You can display anything using this keywords whether it is string value or integer value or float value.

How to use ' echo ' function/keyword to display anything
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com </script>
</head>
<body>
<?php
echo "hello world";   // string value;
echo  10;                   // integer value;
echo   "<script>alert('echo example')</script>";     // java script
?>
</body>
</html>

How to use 'print' function/keyword to display anything
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com </title>
</head>
<body>
<?php
print "hello world";   // string value;
print  10;                   // integer value;
print   "<script>alert('echo example')</script>";     // java script
?>
</body>
</html>

Now you are must thinking both the function are same then what is the difference between them. Actually there is no difference between 'echo' and 'print' function but the 'print' function has one limitation. The 'echo' function can work with more than one string or expression at a time but the 'print' function cannot work with more than one string or expression at a time. This will be more clear after seeing the example below.
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com </title>
</head>
<body>
<?php
echo "Hello","world";  
print "Hello","world";   // this will show you an error
?>
</body>
</html>

The 'print' function can display only one string at a time. The speed of 'echo' function is more tan the 'print' function

Apart from this two display function one more display function is available in php Print_r ( ). This function is used to display the array element. See the below example
<!DOCTYPE html>
<html>
<head><title>expertcoding.com</title>
</head>
<body>
<?php
$example = array('mukesh','prashant','sachin','arpana','ankita');
echo "<pre>";
print_r($example);
?>
</body>
</html>

The above code will show you the output
Array
(
    [0] => mukesh
    [1] => prashant
    [2] => sachin
    [3] => arpana
    [4] => ankita
)

These three function are mainly available in Php to display data in browser

No comments: