Basic PHP Syntax
- A PHP script can be placed anywhere in the document.
- A PHP script starts with <?php and ends with ?>
<?php
// PHP code goes here
?>
- The default file extension for PHP files is ".php".
- A PHP file normally contains HTML tags, and some PHP scripting code.
- Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP 5 Variables
Variables are "containers" for storing information:
<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
$a="poovarasan";
$b="vasudevan";
echo $a." ".$b;
?>
PHP 5 echo and print Statements
In PHP there is two basic ways to get output: echo and print.
In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.
PHP echo and print Statements
There are some differences between echo and print:
- echo - can output one or more strings
- print - can only output one string, and returns always 1
Tip: echo is marginally faster compared to print as echo does not return any value.
The PHP echo Statement
echo is a language construct, and can be used with or without parentheses: echo or echo().
Display Strings
The following example shows how to display different strings with the echo command (also notice that the strings can contain HTML markup):
<?php echo "<h2>PHP is fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?>
0 comments:
Post a Comment