Creating my first PHP file
In this page we will add PHP scripting languaje codes to our regular HTML page, we will learn how to write texts and how to concatenete them and write some special characters, and we will start using variables.

In our fisrt step to learn PHP scripting languaje we will modify a regular HTML page and we will add to it PHP commads.

Bellow we will find a regular HTML page:

mypage.html
<html>
<head>
<title>This is my page</title>
</head>

<body>
This is the content of my page.

</body>

</html>

To create a PHP page using the page above as our model, the first important action is to rename our file. In our case we will name the file "mypage.php". When a file with ".php" extension is placed in the server and the corresponding url is visited, the server will process the file before sending it to the client, so that all PHP commands will be processed. We may just change the name of our file as propoused  and save it to our server (without any additional changes), and when visiting the corresponding URL the result will be exactly the same (because there is nothing to be processed in the page) . 

To this renamed page ("mypage.php") we will add a small PHP command within  <?php opening tag and ?> closing tag.

mypage.php
<html>
<head>
<title>This is my page</title>
</head>

<body>
This is the content of my page.
<?php
print "Do you like it?";
?>


</body>

</html>

When visiting the page, the server will process the page to execute all PHP commands in the page, which will be located within   <?php opening tag and ?> closing tags. In our case  "print" command will  write to the resulting page the text contained between brakets ("Do you like it?"). Often, instead of print command, echo command is used to output string. At the end of the line we will add ";" (otherway we will get an error). We may use "echo" command instead of "print", and the result will be identical.

We may also include all the php commands in one line by placing the code bellow in our page.
 <?php print "Do you like it?"; ?>

Between opening and closing tags we may include several lines with executable commands as shown bellow (we must write ";" at the end of each line):


mypage.php
<html>
<head>
<title>This is my page</title>
</head>

<body>
This is the content of my page.
<?php
print "Do you like it?";
print "<br>";
print "<b>If not, please visit a different page.</b>";
print "<br>";
print "<h1>Goodbay</h1>";
?>

</body>

</html>


Additionaly we may add  several opening and closing tags within our page with executable commands. 



mypage.php
<html>
<head>
<title>This is my page</title>
</head>

<body <?php print "bgcolor=#FFFFFF"; ?>>
<?php
print "Hello!<br>";
?>
This is the content of my page.
<?php
print "Do you like it?";
print "<br>";
print "<b>If not, please visit a different page.</b>";
print "<br>";
print "<h1>Goodbay</h1>";
?>

</body>

</html>

As sown in the example above we may add PHP scripts within HTML tags.

Concatenation of strings

Check the codes bellow:

Code 1
<?php
print "Hello!<br>";

print "I am Joe";

?>
Code 2
<?php
print "Hello!<br>".
"I am Joe";
?>

The pages containing any of the codes above will be exactly the same. We have just put both lines in one, and to do that we have use a point between the two texts we want to show in our pages. The point used in Code 2 will concatenated both strings.

Writing some special characters

Check the codes bellow:

The code
The output
Use "\n" to add a line break
<pre>
<?php
print "AA\nBB\nCC\nDD\nEE";
?>
</pre>
Use "\n" to add a line break
AA
BB
CC
DD
EE
Use "\"" to add brakets<br>
<?php
print "He said \"Hello\" to John";
?>
Use "\"" to add brakets
He said "Hello" to John



Using variables: first step

With PHP we may also use variables. The variables used in PHP will always start with "$", as for example $i, $a, $mydata, $my_name etc. There are some words which we are not allow to use as a name for a variable, but as we probably do not know which ones are they, our best decission will be to use very descriptive variable names:
$my_favorite_song
$my_counter
$username
etc


In the example bellow we have write a PHP page using some variables:

mypage.php
<?php
$my_name="Joe";
$my_hello_text="Hello everybody!";
$year_create=2002;
?>


<html>
<head>
<title>This is <?php print  $my_name; ?>´spage</title>
</head>

<body>

<?php print $my_hello_text; ?>

This page was written in <?php print  $year_create; ?>.
</body>

</html>

In this example, we have defined three variables (line 2, 3 and 4) and we have included the information in the variable latter within the HTML code.

Check those variables carefully: $my_name, $my_hello_text and  $year_create has not been  defined in advance (we have add a value to the variables  the first time they have used them in the file, without letting know the program we will used it in advance), and the values do not contain the same kind of content:

$my_name, $my_hello_text  are strings
$year_create  is a number

We may also use variables in the following ways:

The code
The output
<?php
$year_create=2002;
print  "This page was written in ".$year_create;
?>
This page was written in 2002
<?php
$year_create=2002;
print  "This page was written in $year_create";
 ?>
This page was written in 2002
<?php
$the_text="This page was written in ";
$year_create=2002;
print  $the_text.$year_create;
 ?>
This page was written in 2002
<?php
$the_text="This page was written in ";
$year_create=2002;
print  "$the_text$year_create";
 ?>
This page was written in 2002

PhpTutorial.info