PHP:Hypertext Preporccesor beginner's tutorial

in #php8 years ago (edited)

A Quick Start To PHP

Haden Miles

    PHP is a web developing programming language to help manage operations done on the server side of an application. PHP stands for PHP: Hypertext Proccessor, or Personal Home Page: Hypertext Proccessor. It is an interpenetrated language which means it doesn't compile source code files into an executable file. This helps make testing become much faster as you don't have to recompile each project. PHP is a simple language to learn and is a great first language to learn alongside HTML so you can build your websites. PHP is always great to learn because of it's amazing documentation to understand every nook and cranny of PHP. The PHP manual can be found at http://php.net/

Installation


    To get php to run you need a web server. This can either be purchased from on a web hosting website (this will let the world see) or you can download one for free and install it.

I personally use XAMPP, not for any particular reason but it has always been an install and go with very little configuration. Once you get a web server going you are going to want to go to http://127.0.0.1/ or http://127.0.0.1:####/ where #### is the port number if standard port 80 is blocked. You will see the web servers home page! We ware going to delete this and start making our own content! To do this open up a text editor and your file system explorer. In your explorer go to where you installed the web server and find the web root directory. (For XAMPP this is C:\INSTALL_PATH\xampp\htdocs). You can delete everything in this folder if you'd like, or just the index.php file as this is the one we will replace.
Next open your text editor and make a file named index.php in your web root directory. Next we will talk about the syntax of php so you can start making your web server do things!

Syntax


    PHP has very basic synatx. We will go over each statement and learn more things over time but to start off with we will talk about variables and printing things to the screen.

Starting PHP


    PHP can be embedded inside any file type that is executed as hypertext markup. i.e. HTML, Javascript. To declare that the code is php we write

<?php

and to close it we write

?>

There are other ways of begging php code that can be referenced in the manual.

Variables


    Variables are simple. They are text that stand for other data. PHP has predefined variables, http://php.net/manual/en/reserved.variables.php, most common/ones you'll use first $_POST, $_GET, $_SERVER, and $_SESSION. There are rules on how we can create variables and are as follows.

  • Has to start with $ - Every variable starts with a $ sign to declare that it is indeed a variable
  • $ cannot be followed by anything but an underscore "_" or a letter.
  • The variable can contain numbers but cannot start with one, nor can it contain special characters.
    for example
$_NAME - is ok
$apples - is ok
$99grapes - is NOT ok, starts with number 9
$grapes99 - is ok, can contain numbers

There are a few different variable types but for now we will talk about string, integer, float, boolean, and null

  • Strings are text encased in either double quotes or single quotes. Double quotes are proccessed differently than single quotes. The text inside will be rendered by php when it is executed. "\n" will make a new line whereas '\n' will print the literall \n.
  • Integers are WHOLE numbers, 1, 15, 999, ect. There is a max value these can hold and is equal to 2^(32 or 64)-1. 32 or 64 depending on operating system. Most modern systems are 64 bit.
  • Float is a number with decimal places for increased accuarcy, 1.5, 6.52, 788.9564641, you will lose accuracy after so many decimal places. To learn why visit http://floating-point-gui.de/
  • Boolean is either true or false. They cannot hold other values.
  • NULL has a value of litterally nothing. It doesn't represent true or false (similary to false in ways).

It is important to understand these datatypes and when to use them as they are the foundation to a successful program.

Printing to the Screen


    To print things out to the screen we use a language construct called echo. In your text editor type

<?php
echo "Hello World";
?>

save the file and go to http:/127.0.0.1/ and you should see, Hello World, printed on the page. To help understand how php works, we are going to break this down to each individual line/piece.
1 <?php - tells the server that this is php code
2 echo "Hello World";

  • echo - prints to the screen the following string or evaluation.
  • "Hello World" - it is a string to be printed, double quotes symbolizes text.
  • ; - ends the statement. Every statement you write will be followed by this or you will have a syntax error. It represents the end of an "instruction" for php to execute;

3 <> - closes php

Now we are going to implement variables with echo!

&lt;?php
$currentYear = 2016;
$birthYear = 1997;
echo $currentYear;
echo $birthYear;
?>

This code will prdouce 20161997 on you're page, the reason it combines them is because we didn't tell the page to put a line break in. PHP can echo HTML out to the page in a string format. so between the two echo statements insert echo "<br/>"; and save, refresh, and you should see:

2016
1997

Now what we can do is basic arithmetic on these, add + , subtract - , divide / , and multiply * .

$currentYear = 2016;
$birthYear = 1997;
echo $current-$birthYear;

You should get 19!

echo can also combine strings with variables (if the variable can be converted to a string).

echo "Your Age is:".($currentYear-$birthYear);

This will combine the text with the output of ($currentYear-$birthYear) and then output it to the page. Mess around with Variables a little bit. You can make variables equal to other variables! We'll go over more in my next post and begin if statements, for and while loops, and switch statements.

Hope I can edit and improve this guide as needed and help people learn how to program in php!