Introduction to PHP

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:

  1. Print your name using echo
  2. Print your favorite programming language
  3. Use a comment to explain what your code does
  4. Try using both echo and print
<?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
  • echo and print are used to output data
  • Comments start with // or # for single lines, or /* */ for multiple lines
  • PHP integrates seamlessly with HTML

Next Steps

In the next lesson, we'll learn about variables and data types - how to store and work with different kinds of information in PHP. You'll discover how PHP's dynamic typing makes it easy to work with data.

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.

Introduction to PHP | LearningPHP.org