PHP Syntax (2024)

A PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic PHP Syntax

A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>:

<?php// PHP code goes here?>

The default file extension for PHP files is ".php".

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text"Hello World!" on a web page:

Example

A simple .php file with both HTML code and PHP code:

<!DOCTYPE html><html><body><h1>My first PHP page</h1><?phpecho "Hello World!";?></body></html>

Try it Yourself »

Note: PHP statements end with a semicolon (;).

PHP Case Sensitivity

In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.

In the example below, all three echo statements below are equal and legal:

Example

ECHO is the same as echo:

<!DOCTYPE html><html><body><?phpECHO "Hello World!<br>";echo "Hello World!<br>";EcHo "Hello World!<br>";?></body></html>

Try it Yourself »

Note: However; all variable names are case-sensitive!

Look at the example below; only the first statement will display the value of the $color variable! This is because $color, $COLOR, and $coLOR are treated as three different variables:

Example

$COLOR is not same as $color:

<!DOCTYPE html><html><body><?php
$color = "red";echo "My car is " . $color . "<br>";echo "My house is " . $COLOR . "<br>";echo "My boat is " . $coLOR . "<br>";?></body></html>

Try it Yourself »

PHP Exercises


W3schools Pathfinder

Track your progress - it's free!

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

PHP Syntax (2024)

FAQs

Is PHP syntax hard? ›

In general, PHP is regarded as an easy programming language to master for people just starting to learn to program. As with any programming language, PHP has rules of coding, abbreviations, and algorithms. Learning PHP will be easy or challenging depending on how you approach learning the language itself.

What is correct syntax for PHP code? ›

Basic PHP Syntax

A PHP script starts with <?php and ends with ?> : <?php // PHP code goes here ?>

How to learn PHP fast? ›

The best way to learn PHP syntax and semantics is through constant practice. Write code every day, work on small projects, and gradually challenge yourself with more complex projects. Also, if you're stuck, don't hesitate to ask for help.

Which of the following is the correct way to add a comment in PHP code *? ›

Answer: Which of the following is the correct way to add a comment in PHP code? Explanation: In PHP, /* */ can also be used to comment just a single line although it is used for paragraphs. // and # are used only for single-line comments.

Is PHP more difficult than Python? ›

Learning Curve

Both languages are considered developer-friendly. But if we have to compare them, PHP has a steeper learning curve than Python because it has a bit more complex syntax as well as inconsistent naming conventions.

Is PHP easier than Python? ›

The syntax of a language defines its readability and ease of writing, and in terms of syntax, Python is much simpler and more readable as it resembles a lot with the English language, while PHP has a wide range of conventions which makes it less easy to understand.

Is PHP syntax easy? ›

Reasons why PHP is considered easy to learn: Simple and logical syntax: PHP's syntax is similar to HTML, making it easier to pick up for those familiar with web development.

Is PHP syntax the same as HTML? ›



HTML is a markup language it is used by front-end developers to develop web applications or websites. PHP is a server-side scripting language used for the development of the dynamic application. The result of HTML code is static and remains constant, whereas the result of PHP is dynamic.

What language is PHP syntax based on? ›

Created in 1994 by Rasmus Lerdorf, the very first incarnation of PHP was a simple set of Common Gateway Interface (CGI) binaries written in the C programming language.

Can I learn PHP in 3 days? ›

Learning PHP typically takes around 3 to 6 months, while MySQL takes about 6 to 7 months. Both PHP and MySQL power several widely accessed web applications, including Facebook, Netflix, Uber, and Twitter.

Is PHP harder than JavaScript? ›

Even though PHP is easier to learn, it is capable of producing full-fledged websites. On the other end, we have JavaScript which is more complicated but is one of the most popular languages around. For frontend development, you will definitely choose JavaScript as PHP is only for server-side development.

Can I learn PHP in a week? ›

Step-by-Step Learning Approach

Getting Acquainted with Syntax: The PHP syntax is straightforward, especially for those with a background in languages like C or Java. If you're a beginner, dedicating a few hours daily over a couple of weeks should help in understanding the basics.

Who is the father of PHP? ›

Rasmus Lerdorf is often referred to as the "Father of PHP." He created PHP (Hypertext Preprocessor) in 1994, initially as a set of tools for tracking visits to his online resume. Over time, PHP evolved into a widely-used server-side scripting language, integral to web development and dynamic websites.

What does the P in PHP stand for? ›

PHP was originally an abbreviation of Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

What is the best practice of PHP comments? ›

PHP comments are a best practice.

To review, use single-line comments for short notes to clarify the purpose of a function or a line of code, and use multiline comments for longer notes, such as a more detailed description of a code block or a note to a fellow developer that they can delete later.

What is the hardest language syntax? ›

Arabic, Mandarin Chinese, and Japanese are among the toughest languages for English speakers to learn, particularly due to their complex grammar, writing, and pronunciation systems. Moreover, Mandarin Chinese is also one of the most spoken native languages in the world.

Is PHP syntax similar to C++? ›

C++ and PHP share some similarity in paradigm and syntax. Both are imperative, object-oriented languages and both use C-like syntax, such as similar control structures, curly-brackets for code blocks and semi-colon to terminate statement. But both are very much different in purpose.

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6223

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.