Introduction to PHP
TL;DR — PHP is a free, open-source server-side language that powers over 75% of the web (WordPress, Facebook, Wikipedia). Write your first script in under 2 minutes using the interactive playground below — no installation needed. Every code block on this page is runnable in your browser.
Welcome to your first lesson in PHP programming! PHP (Hypertext Preprocessor) is a server-side scripting language that powers over 75% of websites on the internet, including platforms like WordPress, Facebook, and Wikipedia.
What You'll Learn
- What PHP is and why it's essential for web development
- How to write your first PHP program
- Basic syntax and output methods
- How to run PHP code
What is PHP?
PHP is a server-side scripting language designed specifically for web development. Created by Rasmus Lerdorf in 1994, PHP has evolved into a powerful and versatile language that makes building dynamic websites straightforward and efficient.
Key Features
- Server-side: Runs on the server before HTML is sent to the browser
- Easy to learn: Simple syntax that's beginner-friendly
- Powerful: Can handle everything from simple forms to complex web applications
- Open source: Free to use with a huge community and ecosystem
- Database friendly: Excellent integration with MySQL, PostgreSQL, and other databases
Your First PHP Program
Every PHP script starts with <?php and can optionally end with ?>. Let's start with the classic "Hello, World!" program:
<?php
echo "Hello, World!\n";
When you run this code, you'll see "Hello, World!" in the output. The echo statement is your primary tool for displaying output in PHP. The \n creates a new line.
Understanding Output in PHP
PHP has several ways to output data. Here are the most common:
<?php
// Using echo (most common)
echo "Welcome to PHP!\n";
// You can output multiple strings
echo "Hello", " ", "PHP", "!\n";
// Using print (similar to echo)
print "Learning PHP is fun!\n";
// Using print_r for arrays and objects (we'll learn more later)
print_r([1, 2, 3]);
echo "\n";
// Using var_dump to show detailed information
var_dump("PHP");
Best Practice: Use echo for simple output. Use print_r() and var_dump() for debugging arrays and objects.
Comments in PHP
Comments help you explain your code. PHP supports three types of comments:
<?php
// This is a single-line comment
echo "This will run\n"; // Comment at the end of a line
# This is also a single-line comment (Unix-style)
echo "PHP has multiple comment styles\n";
/*
This is a
multi-line comment
that spans several lines
*/
echo "Comments are ignored by PHP\n";
PHP and HTML
One of PHP's strengths is how easily it integrates with HTML. PHP code can be embedded directly in HTML files:
<?php
$greeting = "Hello from PHP";
?>
<!-- HTML would go here in a real file -->
<?php echo $greeting; ?>
In web development, you'd save this as a .php file and the server would process the PHP code before sending HTML to the browser.
Try It Yourself
Now it's your turn! Modify the code below to:
- Print your name using
echo - Print your favorite programming language
- Use a comment to explain what your code does
- Try using both
echoandprint
<?php
// Your code here
echo "Hello, PHP!\n";
// Try adding more echo statements
?>
Key Takeaways
- PHP is a server-side scripting language perfect for web development
- All PHP code starts with
<?php echoandprintare used to output data- Comments start with
//or#for single lines, or/* */for multiple lines - PHP integrates seamlessly with HTML
Next Steps
You can now write PHP scripts, produce output, and add comments to explain your code. These are the building blocks every PHP program rests on.
Right now every value you print is hard-coded directly in an echo statement. Real applications need to remember things -- a user's name, a product price, the result of a calculation. That is exactly what variables and data types give you, and understanding them unlocks everything that follows: functions, control flow, databases, and beyond.
Continue to Variables and Data Types -->
Pro Tip: PHP is best learned by doing! Try modifying the examples above and see what happens. Don't worry about making mistakes - that's how you learn. Consider installing PHP locally or using an online sandbox like OnlinePHP.io to run your code.