Difference between revisions of "PHP Basics"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(PHP Comments)
Line 63: Line 63:
 
//Hello World
 
//Hello World
 
//Hola Mundo
 
//Hola Mundo
 +
</syntaxhighlight>
 +
 +
 +
==PHP Variables==
 +
In PHP you don't declare variables before they are used, instead you just assign it a value and PHP will automatically convert it to the correct data type in the background. All variables in PHP start with the dollar sign ($) followed by the name of the variable. The name must start with a letter, and can only contain alpha-numeric characters and underscore. Variables are also case sensitive.
 +
 +
<syntaxhighlight lang="php" line>
 +
<?php
 +
$name = "wayne";
 +
$age = 21;
 +
$alive = true;
 +
?>
 +
</syntaxhighlight>
 +
 +
<syntaxhighlight lang="php" line>
 +
<?php
 +
$message = "<p> Hello World <br> Hola Mundo </p>";
 +
echo $message;
 +
?>
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 23:55, 16 December 2016

This section needs expansion.
You can help by adding to it.

PHP is a server side scripting language, which is run on the server before the page is served to the client. Essentially it dynamically creates pages on the fly. It can be written on it's own our within normal standard HTML etc. It must be stored on a running webserver and you can only access it via it's URL in a browser. The page the client will receive will have all of the server side elements replaced with the output of the code. If your viewed source you would only see standard HTML and text.

PHP Tags

PHP code must be contained within PHP tags, these tags can be placed anywhere in the page because they are all run before the page is served to the client.

1 <?php // the opening PHP tag
2 // your PHP code
3 ?> // the closing PHP tag

Within your PHP tags every line must be terminated with a semi colon.

PHP Comments

Like all languages PHP supports comments, these can greatly help the maintenance of a project because the developer / programmer may not be the person who maintains the project when it is live. The comments are exactly the same as in C#:

1 // This will comment a single line or include a comment at the end of that single line

Alternatively:

1 /*
2 This will comment all lines
3 between the start and
4 the end comment marks
5 */

Creating a Hello World in PHP

1 <?php echo "hello world"; ?>
2 //Output:
3 //hello world

echo is used to write text to the page, you could also echo html code or variables:

1 <?php echo "<p>Hello World<br>Hola Mundo</p>"; ?>
2 //Output:
3 //Hello World
4 //Hola Mundo

You could also do this:

 1 <html>
 2 <head> <title> Hello World </title> </head>
 3 <body>
 4 <p>
 5 <?php echo "Hello World"; ?>
 6 <br>
 7 <?php echo "Hola Mundo"; ?>
 8 </p>
 9 </body>
10 </html>
11 //Output:
12 //Hello World
13 //Hola Mundo


PHP Variables

In PHP you don't declare variables before they are used, instead you just assign it a value and PHP will automatically convert it to the correct data type in the background. All variables in PHP start with the dollar sign ($) followed by the name of the variable. The name must start with a letter, and can only contain alpha-numeric characters and underscore. Variables are also case sensitive.

1 <?php 
2 $name = "wayne";
3 $age = 21;
4 $alive = true; 
5 ?>
1 <?php 
2 $message = "<p> Hello World <br> Hola Mundo </p>"; 
3 echo $message; 
4 ?>