Coding PHP Scripts


  • You can use any text editor to create PHP scripts.
  • PHP scripts should be saved in files with the .php extension.
  • PHP scripts should be coded within <?php and ?> tags.
  • In PHP scripting statement terminator is “;” symbol. Thus each statement in a script should be terminated from the symbol ;
  • You may have multiple statements in a single physical line, but having multiple statements in a single physical line is not a good practice.
  • PHP is a free format language. Thus you can have spaces in between statement components as you wish.


Printing text on the screen

The echo command can be used to print text on the screen. The syntax of the echo command is given below
echo “some text”;

Type the following script in a file by using a text editor and save it with the name “example.php”

<?php
echo “My first phpscript”;
?>

Execute the script at the DOS command line by typing the command php example.php

Embedding a PHP Script in a HTML Page


<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>

When the server encounters the PHP tags it switches from the HTML to PHP mode.

There are four different ways to embed the PHP code

1.<?php echo(“Some PHP code”); ?>
2.<? echo(“Some PHP code”); ?>
3.<SCRIPT Language=‘php’> echo(“Some PHP code”); </SCRIPT>
4.<% echo(“Some PHP code”); %>

PHP Data Type

Three basic data types
* Integer
* Double
* String

More data types
* Array
* Object

PHP is an untyped language, so variables type can change on the fly.

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.


PHP Constants

* Values that never changes.
* Constants are defined in PHP by using the define() function.
* For e.g.define(“NCST”, “National Centre for Software Technology”)
* defined() function says whether the constant exists or not.




Related Articles