Learn PHP Step by Step | PHP.Exalogics.com
Every PHP developer starts with a simple program. It may only display a few words on the screen, but it introduces the basic syntax of PHP and demonstrates how server-side programming works.
In this lesson, you will write your first PHP script, understand the structure of a PHP file, learn how the echo statement works, and discover some of the most important rules every PHP programmer should know.
Creating Your First PHP File
Open your project folder and create a new file called:
index.php
Remember that PHP files must use the .php extension.
Now type the following code:
<?php
echo "Hello World!";
?>
Save the file.
Open your browser and visit:
http://localhost/yourproject/
You should now see:
Hello World!
Understanding the Code
Although this program contains only one line of PHP code, each part has a specific purpose.
<?php
echo "Hello World!";
?>
<?php
This tells the web server that PHP code begins here.
echo
The echo command sends text to the browser.
“Hello World!”
This is a string, which simply means text enclosed inside quotation marks.
;
The semicolon marks the end of a PHP statement.
?>
This marks the end of the PHP block.
Good Practice
In files that contain only PHP code, many developers omit the closing ?> tag. This helps prevent accidental spaces or blank lines from being sent to the browser.
How PHP Executes
When you request a PHP page, several things happen before you see the final result.
- Your browser requests the page.
- The web server receives the request.
- The PHP interpreter executes the PHP code.
- HTML output is generated.
- The browser displays the finished page.
The visitor never sees the PHP code itself. Only the generated output is sent to the browser.
Using echo
The echo statement is one of the most frequently used commands in PHP.
Example:
<?php
echo "Welcome to PHP.Exalogics.com";
?>
Output:
Welcome to PHP.Exalogics.com
Displaying Multiple Lines
<?php
echo "Welcome";
echo "<br>";
echo "Learning PHP is fun!";
?>
Output:
Welcome
Learning PHP is fun!
The <br> tag creates a new line because PHP outputs HTML to the browser.
Using HTML Inside PHP
PHP can generate complete HTML pages.
<?php
echo "<h1>Welcome</h1>";
echo "<p>This paragraph was created by PHP.</p>";
?>
The browser displays a heading followed by a paragraph exactly as if you had written normal HTML.
Comments in PHP
Comments help explain your code. They are ignored by PHP and are never shown to visitors.
Single-line comment:
<?php
// This is a comment
echo "Hello";
?>
Multi-line comment:
<?php
/*
This is
a multi-line
comment.
*/
echo "Hello";
?>
Well-commented code is easier to maintain, especially in large projects or when working in a team.
Mixing PHP and HTML
One of PHP’s biggest strengths is that it integrates seamlessly with HTML.
<html>
<body>
<h1>My Website</h1>
<?php
echo "<p>Today's date is displayed here.</p>";
?>
</body>
</html>
This allows developers to create dynamic web pages while keeping the HTML structure clean and easy to understand.
Common Beginner Mistakes
| Mistake | Solution |
|---|---|
| Saving as .html | Save as .php |
| Missing semicolon | Add ; at the end of every statement |
| Using smart quotes | Use standard keyboard quotation marks |
| Opening file directly | Use http://localhost/ |
| Apache not running | Start Apache first |
Every PHP programmer has forgotten a semicolon at some point. Even experienced developers encounter syntax errors. Learning to read error messages is an important skill that will help you solve problems quickly.
echo vs print
Both echo and print display information in the browser. In most situations either can be used, but there are a few differences.
| echo | |
|---|---|
| Can output one or more strings. | Outputs one string. |
| Slightly faster. | Slightly slower. |
| Does not return a value. | Returns the value 1. |
Example using print:
<?php
print "Welcome to PHP!";
?>
Most developers prefer using echo because it is simple, flexible and commonly seen in PHP projects.
PHP Syntax Rules
PHP is an easy language to learn, but following a few basic rules will prevent many common errors.
- Every PHP program starts with <?php.
- Most statements end with a semicolon (;).
- Strings should be enclosed in quotation marks.
- PHP files should use the .php extension.
- Run PHP files through a web server such as Apache or Nginx.
Case Sensitivity
PHP treats some elements as case-sensitive and others as case-insensitive.
Keywords
Keywords such as echo, if, and while are not case-sensitive.
<?php
ECHO "Hello";
echo "World";
EcHo "!";
?>
Although this works, professional developers always use lowercase keywords because they are easier to read.
Variables
Variable names are case-sensitive.
<?php
$name = "John";
echo $name;
echo $Name;
?>
Here, $name and $Name are two different variables.
Using Whitespace
PHP ignores extra spaces, tabs and blank lines in most situations.
These two examples produce exactly the same result.
<?php
echo "Hello";
?>
<?php
echo "Hello";
?>
However, writing neat and consistent code makes your programs much easier to understand.
Coding Style Tips
Professional developers spend just as much time reading code as writing it. Clean code saves time and reduces errors.
- Use meaningful file names.
- Indent your code consistently.
- Add comments where necessary.
- Keep functions short.
- Avoid repeating code.
- Use descriptive variable names.
- Test your code regularly.
Real World Example
Suppose you operate a travel company. Instead of writing every page manually, PHP can generate content automatically.
<?php
echo "<h2>Travel & Culture Services</h2>";
echo "<p>Welcome to our Pakistan tour website.</p>";
echo "<p>We offer cultural tours, trekking, hotel reservations and transport services.</p>";
?>
As your website grows, this approach allows you to display information dynamically from a database instead of editing hundreds of HTML pages.
Practice Exercises
Try the following exercises before moving to the next lesson.
- Create a PHP file that displays your name.
- Display today’s date using PHP.
- Use echo to create an HTML heading.
- Display three paragraphs using separate echo statements.
- Add comments explaining your code.
Experimenting with small programs is one of the best ways to become comfortable with PHP syntax.
Summary
You have now written your first PHP program and learned how PHP executes code on the server before sending HTML to the browser. You also learned about the echo statement, comments, PHP tags, syntax rules, and common beginner mistakes.
These fundamentals may seem simple, but they form the foundation for everything you will build in PHP. Every dynamic website, web application, API, or content management system starts with these basic concepts.
Frequently Asked Questions
Do PHP files always end with .php?
Yes. Web servers use the .php extension to determine that the file should be processed by the PHP interpreter.
Can I write HTML inside PHP?
Yes. PHP and HTML work together seamlessly. PHP can generate HTML dynamically or be embedded within an HTML page.
Why doesn’t my PHP code run?
Common reasons include saving the file with the wrong extension, opening it directly instead of through a web server, or forgetting to start Apache or another web server.
Should I use echo or print?
Both work well, but echo is more commonly used in modern PHP applications.
Can I write PHP without installing a web server?
Traditional PHP web applications require a web server. For command-line scripts, PHP can also be run directly from the terminal using the PHP CLI.
Quick Challenge
Create a page named about.php that displays:
- Your name
- Your city
- Your favourite programming language
- A heading and two paragraphs using HTML generated by PHP
This simple exercise will help reinforce the concepts you’ve learned in this lesson.
Next Tutorial
Now that you understand basic PHP syntax, it’s time to learn about one of the most important concepts in programming: variables.
Next: PHP Variables