Php Syntax For Beginner

Php Syntax For Beginner

Hi everyone and welcome to another tutorial. Today you will learn practical stuff in PHP. You will get your hand dirty munching your keyboard and at the end of this tutorial, you will learn basic PHP syntax and also you will write your first PHP program.

Before we proceed you will need to set up your coding environment if you haven’t because PHP is a server-side programming language. We need to download and install some free software that provides a server-like environment to practice and test our PHP code.

There are many software packages that provide a server-like environment but I recommend Xampp. Xampp in my opinion is free, easy to install, easily configured, and beginner-friendly. It also cross-platform (i.e has a different package for the three main operating systems).

If you don’t have Xampp installed download it here and install it. And if you already have another package like WAMP, LAMP or MAMP installed you can also follow along. Make sure to download the software before you continue so that you can follow along. Also, ensure you have a code editor installed; whichever editor you feel comfortable with but I recommend VS code. You can download VS code here.

After downloading and installing Xampp. Open the Xampp software. And follow the following steps.

Go to your mean drive and you will find a new folder named Xampp.

xampp.png

Double click to open the Xampp folder. Inside the Xampp folder search for a folder named htdocs. The htdocs is where you will be saving all your PHP code to be tested by the local server.

htdocs.png

TEST LOCAL SERVER

Open the installed program Xampp and click start to switch on Apache.

xampstart.png

After clicking switching on Apache server. The admin button beside the start button will be activated; click the admin button to access server content on the web browser.

xamppadmin.png

ESCAPING TO PHP/ PHP DELIMITER

Escaping to PHP a way to separate PHP codes from others. PHP parse engine needs a way to differentiate PHP code from others and this process is referred to as escaping to PHP or PHP delimiter.

There are roughly about four to five ways of escaping to PHP. We will discuss only two which are beginner-friendly and are mostly used. You don’t need to bother yourself with others as you might never really use them.

· Canonical PHP tags are the most used PHP delimiter globally among PHP developers.

Here is how it’s written.

<?PHP phpcodes comes in between this tags  ?>

· HTML script tags: Below is how it's written.

<script language=”php” > phpcodes comes in between this tags </script>

COMMENTING PHP

Imagine you been employed by a tech company and your first task is to add a status feature to the product. Without the codebase been commented out you will have difficulties finding out where you need to add your code to and how to prevent breaking the running product. Comment in PHP program is a way to document your product to ease future use. The comments are not been considered when the program is been run (i.e the comment do not affect the running of the program as they are been ignored).

There are two ways of adding a comment

· Single line comment.

<?PHP

// The double forward slash is one way of commenting single line

# The hashtag is another way of single line commenting in PHP program

// This program print on the screen hello world
echo "Hello World";

#This program print Hi on the screen in the PHP program
echo "Hi";

?>

· Double line comment.

<?php

/* Forward and an asterisk with an asterisk and a forward slash at the end is used for commenting 
out the double lines in a PHP program*/

?>

PHP IS SPACE INSENSITIVE

PHP is space insensitive simply means no matter the length of space between any character in a PHP program it is regarded as a single space.

<?php

/* It Doesn't matter how much space you left between the variable below it will be translated into one space in a PHP program */

/*The two examples below are the same despite the whitespace between Yemisi and Daniel in the first one*/

$name = "Yemisi                                 Daniel";

$name = "Yemisi Daniel";

?>

CASE SENSITIVE

This simply means that in the PHP program “Case” is different from “case” because the first character is of the first spelling is different from the first character of the second spelling. In PHP this spelling will be dealt with as two different things.

<?php

/* PHP is case sensitive where the following variables below will be considered different because the first variable character is in lowercase while the second is in uppercase */

$surname = "Yahaya";

$Surname = "Yahaya";

?>

PHP STATEMENT

PHP statement is any expression that is followed by a semi-colon. In PHP every statement must be ended with a semi-colon or the program will give an error while it's been run.

<?php

# PHP statement

$surname = "Yahaya";

?>

PHP FILE EXTENSION.

Every file type as a format been used in storing it on a computer. For example, images are saved using the .jpeg .png. .gif extension. In PHP it is also compulsory you use the PHP extension to save your file or else it won’t work. The extension for saving PHP files is ".php".

YOUR FIRST PHP PROGRAM

In your first program, you will be writing a program that prints whatever message you want to the screen of the computer.

<?php

// First PHP program, put whatever you want to be printed on the screen in between the double quotes

echo = "";

?>

TESTING YOUR CODE ON LOCAL SERVER

Now you have written your first program and will like to test it on your locally installed server environment. I do advise you to create a new folder in the htdocs and name it Projects. Use the projects folder to store all your code to be tested. In the projects folder create another folder called “My first PHP program”. Open a file in the My first PHP program folder put your code In and save it as "index.php".

To see your code in action open your web browser and click the address bar. Enter the URL to your file. it goes like this "localhost/name of the folder or file in htdocs. If you follow my instruction above. In your case it should be "localhost/Projects/My first PHP Program". Make sure the spellings are the same to avoid frustrations.

CONCLUSION

Today you have learned the very basics of PHP programming which are the syntax in every PHP program and most importantly you have written your first PHP program. You have come to a long way and congratulations.

If you experience any difficulty feel free to comment with your question or reach out to me on Twitter I do be glad to help you out .