Writing a code using php?
Before we jump into coding we need to learn few basic things. Once you installed php on your machine and ready to write your first program using php you need to learn about the syntax.
Every language has their own syntax. Syntax is basically a way to identify a language. Followings are some of the way you can write php code.
- default tag i.e. <?php ?>
- short open tag <? ?>
- asp style tag . <% %>
- html script tag . <script language="php"></script>
When you install php by default you can only use default tag and html script tag. In order to use other tags you have to enable the support for these tags in php.ini configuration file.
Note: The ASP tags
<%=, and the script tag
<script language="php"> are removed from PHP7.
What is php.ini?
The configuration file (php.ini) is read when php application starts. It picks up the limitations, extensions and some other configuration defined in the file and work accordingly.
How to write first php code?
To write your first php code you need to have following things setup:
- You must have php installed
- You must have php available globally
- You must have a text editor to write code
There are two types of programs that you can write using php:
- console application
- web application
Console applications can be run using command line or terminal. Web applications can be run using web browsers like chrome, safari , firefox etc..
Before we start writing web applications we first need to learn some basics of php and that is why we will learn how to write console apps first.
What is .php file extension?
In php language php code is written in a file with .php extension. Using .php extension you can easily know that program is written in php language.
How to write php command line script?
To write your first command line script:
- make sure you have php installed globally
- create a file called test.php
- open this file in an editor of your choice on your computer
How to write something on terminal or browser?
There are few php functions that you can use to write something on your terminal screen or web browser. Let's learn some of the important functions that can help you write something on screen.
You need to know how many ways you can write in php. To describe your code you need to learn about comments as well. Comments basically describes what your code is doing so that other developers can easily debug your code.
Types of comments in php
Following is a sample php code that defines different types of comments that you can write using php language. In php comments are not displayed on a screen.
<?php // this is a single line comment /* This is a multi-line comment Code written in comments tags will not be visible to user */ <!-- This is an HTML Style Comment --> ?>
A good developer always write comments regarding his code so that other developer can easily read his code. Although it is not necessary to describe each line of code.
Only write comments where it is required. Through out my php tutorials I will put comments a lot so that you can easily read my code.
How to display text on browser or terminal?
To write something on terminal screen or web browser you need to learn about different php functions that we can use to display data.
Followings are some of the ways you can output something on the screen. Open your newly created test.php file and paste following contents:
<?php // echo function echo "This is my first code. "; // print function print "This is a second line."; // writing multiple lines print <<<END You can easily write multiple lines of code using heredoc syntax it starts with triple (<) followed by a keyword. END; ?>
Save your test.php file and open a terminal window and navigate to the folder where you saved this file and run following command to run php code on your terminal screen.
When you run above command you will see following output on your terminal window:
What did we learn today?
- we learned that we can write php using diffrerent tag
- all tags are not available during php installation and can be enabled using php.ini file
- ways to write different comments
- ways to print texts on a browser or terminal
Things to remember
- every statement in php must end with ; symbol
- php code must be wrapper with php tags Example: <?php ?>
- write comments to describe your code so that it is readable by other developers easily
- to run script using command line you must prefix the file name with php i.e . php test.php
News and Updates
Subscribe to our newsletter and receive the latest news from TheThemeio.
- Excercise for Variables
- PHP Variables
- Github Pages
- Remote Data
- What is AngularJS?
- Model and Controller
- Organizing AngularJS