Skip to main content
March 23, 2026Noble Desktop Publishing Team/9 min read

Basic PHP Syntax: Free PHP & MySQL Tutorial

Master PHP fundamentals with hands-on syntax examples

Core PHP Concepts You'll Master

Variables & Echo

Learn to create PHP variables and output content to web pages using the echo command.

String Handling

Master single vs double quotes, escaping characters, and string concatenation techniques.

Development Environment

Set up and use MAMP PRO or XAMPP for local PHP development on Mac and Windows.

Topics Covered in This PHP & MySQL Tutorial:

Echo, Strings, & Variables, Single Quotes Vs. Double Quotes, Escaping Characters, Heredoc, Concatenation, Comments

Exercise Overview

This exercise establishes your foundation in PHP syntax fundamentals. If you're experienced with a language like JavaScript, many of these concepts will feel familiar, but PHP's unique characteristics make it worth understanding thoroughly. Even without prior programming experience, you'll discover that PHP's intuitive design makes it one of the more accessible server-side languages to master. This hands-on approach will give you the confidence to tackle more complex PHP development challenges.

Programming Background

If you're familiar with JavaScript, much of PHP syntax will look familiar. Even without previous programming experience, PHP is designed to be simple to learn.

Mac: Launching MAMP PRO

  1. If MAMP PRO is not already running, navigate to Hard Drive > Applications > MAMP PRO and double–click MAMP PRO.app. MAMP PRO remains the preferred local development environment for Mac users due to its stability and professional features.

  2. MAMP PRO will launch and typically opens the MAMP start page in your default web browser automatically. This dashboard provides access to your local development environment and server configuration.

    If the start page doesn't appear automatically, click the WebStart button in the MAMP PRO application (you may need to click the Start button first), or navigate directly to localhost:8888/MAMP in your browser.

Starting MAMP PRO on Mac

1

Launch Application

Navigate to Hard Drive > Applications > MAMP PRO and double-click MAMP PRO.app

2

Access Web Interface

Click WebStart button or navigate to localhost:8888/MAMP in your browser

Windows: Launching XAMPP

  1. If XAMPP is not currently running, navigate to the C: > xampp folder and double–click xampp-control.exe. XAMPP continues to be the most reliable cross-platform solution for Windows PHP development.

  2. Next to Apache click Start to initialize your web server.

  3. Next to MySQL click Start to activate your database server. Both services must be running for full PHP functionality.

Now that your development environment is configured, let's dive into the core PHP concepts that form the backbone of modern web development.

Starting XAMPP on Windows

1

Open Control Panel

Navigate to C: > xampp folder and double-click xampp-control.exe

2

Start Services

Click Start next to Apache, then click Start next to MySQL

Echo, Strings, & Variables

  1. In your code editor, open first.php from the phpclass folder. This folder is located in your htdocs folder at these locations:

    • Mac: Hard Drive > Applications > MAMP > htdocs
    • Windows: C: > xampp > htdocs
  2. Between the <body> tags, add the following code:

    <body>
    <?php 
       echo "Hello Universe";
    ?>
    </body>

    Let's examine this fundamental PHP structure. The <?php and ?> tags create a clear boundary between PHP code and HTML content. Everything within these tags is processed by the PHP interpreter on the server before the page reaches the browser. This server-side processing is what makes PHP so powerful for dynamic web applications.

    The echo command is PHP's primary output function, sending content directly to the browser. Here we're outputting a string enclosed in double quotes, which tells PHP to treat the content as literal text.

    The semicolon (;) is crucial—it marks the end of each PHP statement. Forgetting semicolons is one of the most common sources of syntax errors for new PHP developers, so developing this habit early will save you debugging time later.

  3. Save the file using Ctrl+S (Windows) or Cmd+S (Mac).

  4. Open your browser and navigate to:

    • Mac: localhost:8888/phpclass/first.php
    • Windows: localhost/phpclass/first.php

    The page should display:

    Hello Universe

  5. PHP's echo function can also evaluate mathematical expressions. Return to your code editor to explore this capability.

  6. Delete everything between the <?php ?> tags and replace it with:

    <?php 
       echo 2 + 2;
    ?>

    Notice the absence of quotes around 2 + 2. Without quotes, PHP interprets this as a mathematical expression rather than a text string. This distinction between data types is fundamental to PHP programming.

  7. Save the file and refresh your browser page:

    • Mac: localhost:8888/phpclass/first.php
    • Windows: localhost/phpclass/first.php

    The page should now display:

    4

  8. Keep the browser tab open for quick testing as we continue making changes.

  9. Now let's observe how quotes change PHP's behavior. Switch back to your code editor.

  10. Add quotes around the expression as shown:

    <?php 
       echo "2 + 2";
    ?>
  11. Save the file and reload first.php in your browser.

    The page should now display:

    2 + 2

    The quotes instruct PHP to treat the content as a literal string rather than executable code. Understanding when to use quotes versus when to omit them is essential for controlling how PHP processes your data.

  12. Variables are where PHP's real power begins to emerge. Return to your code editor.

  13. Replace everything between the <?php ?> tags with:

    <?php 
       $myMessage = "Hello Universe";
       echo $myMessage;
    ?>

    Remember: semicolons are required at the end of each statement!

  14. Save the file, return to your browser, and reload first.php.

    The page should display:

    Hello Universe

    You've just created your first PHP variable. All PHP variables begin with the dollar sign ($), which distinguishes them from other elements in your code. The variable $myMessage now stores the string "Hello Universe" in memory, ready for reuse throughout your script.

    Critical Note: PHP variables are case-sensitive, meaning $mymessage and $myMessage are completely different variables. This sensitivity extends to all PHP identifiers, so consistent naming conventions are essential for maintainable code.

Understanding how PHP handles different quote types is crucial for string manipulation and avoiding common pitfalls in professional development.

Critical Syntax Rule

Always end PHP statements with a semicolon (;). Forgetting semicolons will cause errors in your code.

PHP Code vs HTML Output

FeaturePHP CodeBrowser Output
Stringecho "Hello Universe";Hello Universe
Math Expressionecho 2 + 2;4
Math as Stringecho "2 + 2";2 + 2
Recommended: Remove quotes to evaluate expressions, add quotes to display literal text

Single Quotes Vs. Double Quotes

PHP treats single and double quotes differently, and this distinction affects variable parsing and performance. Professional developers leverage these differences strategically.

  1. Return to your code editor to explore these differences.

  2. Modify the echo statement to use single quotes around the variable:

    $myMessage = "Hello Universe";
    echo '$myMessage';
  3. Save the file and reload first.php in your browser.

    The page should display:

    $myMessage

    Single quotes treat everything within them as literal text—no variable parsing occurs. This behavior is actually more efficient when you don't need variable interpolation, as PHP doesn't need to scan the string for variables.

  4. Now let's test double quotes with the same variable. Return to your code editor.

  5. Change the single quotes to double quotes:

    $myMessage = "Hello Universe";
    echo "$myMessage";
  6. Save and reload the page in your browser.

    The page should display:

    Hello Universe

    Double quotes enable variable interpolation—PHP automatically parses and substitutes variable values within the string. This feature makes double quotes incredibly useful for creating dynamic content that combines variables with static text.

  7. Let's demonstrate this flexibility with a practical example. Return to your code editor.

  8. Extend the string to include additional text:

    $myMessage = "Hello Universe";
    echo "$myMessage, nice to meet you.";
  9. Save and reload the page.

    The page should display:

    Hello Universe, nice to meet you.

    This demonstrates the power of double-quoted strings for creating dynamic, personalized content—a cornerstone of modern web applications.

When working with quotes within strings, you'll need to master character escaping to avoid syntax errors that can break your applications.

Quote Types in PHP

FeatureSingle QuotesDouble Quotes
Variable ProcessingLiteral text onlyEvaluates variables
Example Output$myMessageHello Universe
Use CaseStatic stringsDynamic content
Recommended: Use double quotes when mixing variables with strings for more flexibility

Escaping Characters

  1. Return to your code editor to explore character escaping.

  2. Replace everything between the <?php ?> tags with:

    <?php 
       echo 'It's nice to meet you.';
    ?>
  3. Save and reload first.php in your browser.

    The page will generate a syntax error. PHP interprets the apostrophe in "It's" as the closing quote, leaving the remaining text unparseable. This is a classic beginner mistake that's easily resolved through proper escaping.

  4. Return to your code editor to fix this issue.

  5. Add a backslash before the apostrophe:

    echo 'It\'s nice to meet you.';
  6. Save and reload the page.

    The page should now display:

    It's nice to meet you.

    The backslash tells PHP to treat the following character literally rather than as a syntax element.

  7. The same principle applies to double quotes within double-quoted strings. Return to your code editor.

  8. Replace the line with this double-quote example:

    echo "Tell him I said \"Hi\".";
  9. Save and reload the page.

    It should display:

    Tell him I said "Hi".

    PHP provides several escape sequences for common formatting needs: \\ produces a literal backslash, \t creates a tab character, \n generates a line break, and \r produces a carriage return. These sequences are particularly useful when generating formatted text files or email content.

Concatenation—the process of joining strings and variables—is essential for building dynamic content and is fundamental to professional PHP development.

Common Escape Characters

Quotes

Use backslash before quotes: \'It\'s\' for single quotes, \"Hi\" for double quotes.

Special Characters

Use \\ for backslash, \t for tab, \n for new line, \r for carriage return.

Why Escaping Matters

PHP interprets quotes as string delimiters. Without escaping, quotes inside strings confuse the parser and cause errors.

Concatenation

PHP offers multiple concatenation methods, each suited for different scenarios. Understanding these techniques will make your code more efficient and readable.

  1. Return to your code editor to explore concatenation syntax.

  2. Replace everything between the <?php ?> tags with:

    <?php 
       echo "This ". "is ". "my string.";
    ?>

    Pay careful attention to the spacing within each quoted section—the spaces are necessary for readable output.

  3. Save and reload first.php in your browser.

    It should display:

    This is my string.

    The period (.) operator joins multiple string elements into a single output. This explicit concatenation method gives you precise control over spacing and formatting.

  4. The concatenation assignment operator provides even more flexibility for building complex strings. Return to your code editor.

  5. Replace the code with this more advanced example:

    <?php 
       $msg = "I like carrots.";
       $msg .= " And broccoli.";
       echo $msg;
    ?>
  6. Save and reload the page.

    It should display:

    I like carrots. And broccoli.

    The .= operator appends new content to the existing variable value. This shorthand notation is invaluable when building strings incrementally, such as generating HTML content or constructing database queries. You'll encounter this pattern frequently in professional PHP applications.

Proper commenting is crucial for maintaining professional-grade PHP applications, especially when working in team environments or returning to code months later.

Concatenation Methods

FeatureMethodExampleResult
Dot Operator"This " . "is " . "text"This is text
Append Operator$msg .= " more text"Adds to existing variable
Recommended: Use .= operator for efficient string building and variable modification

Comments

  1. Return to your code editor to explore PHP's commenting syntax.

  2. To disable a single line of code, add // at the beginning:

    $msg = "I like carrots.";
    //$msg .= " And broccoli.";
    echo $msg;

    Most modern code editors will visually distinguish commented code by changing its color or making it appear grayed out, providing immediate visual feedback that the line won't execute.

  3. For multi-line comments or temporarily disabling code blocks, use /* and */:

    /*$msg = "I like carrots.";
    //$msg .= " And broccoli.";
    echo $msg;*/

    The entire block becomes inactive, allowing you to quickly test different code paths or leave detailed explanations for other developers. This multi-line syntax is particularly useful for adding comprehensive documentation headers to functions and classes.

  4. Save and close the file. You've successfully completed the fundamental PHP syntax exercises that form the foundation of professional PHP development.

These core concepts—echo statements, variable handling, quote management, character escaping, concatenation, and commenting—represent the building blocks of virtually every PHP application. Mastering these fundamentals positions you to tackle more advanced PHP features with confidence and write maintainable, professional-quality code.

PHP Comment Syntax

Single Line Comments

Use // at the beginning of a line to comment out individual statements.

Multi-line Comments

Use /* to start and */ to end block comments spanning multiple lines.

Key Takeaways

1PHP code must be enclosed within <?php and ?> tags to separate it from HTML content
2All PHP variables start with a dollar sign ($) and are case-sensitive
3Every PHP statement must end with a semicolon (;) to avoid syntax errors
4Double quotes evaluate variables within strings, while single quotes treat everything as literal text
5Use backslash (\) to escape special characters like quotes within strings
6The echo command outputs content to the webpage and can handle both strings and mathematical expressions
7Concatenation uses the dot (.) operator to join strings, and the .= operator to append to existing variables
8Comments use // for single lines and /* */ for multiple lines to document code without execution

RELATED ARTICLES