Run our First PHP Program

Run our First PHP Program

Hi, everyone, today we’ll start our first PHP program. Let’s make our first php application.

File Structure:

  1. Let’s create a folder in ypur xampp/htdocs named phpclass.
  2. Then inside that folder create a file called class1.php.
  3. And this .php is the extension of PHP file.
  4. Every php file, we must have to declare as .php. PHP interpretor can interpret the file if it sees a .php file.

PHP Code:

So, in our C:/xampp/htdocs/phpclass/class1.php would be like this -

<?php
    echo "Hello World. Welcome to PHP";
?>

Run on Browser

Now, go to your browser and run - http://localhost/phpclass/class1.php

And you’ll see this text in the browser -

Hello World. Welcome to PHP

Awesome, we’ve made our first PHP program and view that on the browser.

PHP Code with HTML:

So, after our first PHP program, it’s great right. Let’s improve it a little bit. PHP is a scripting language, we know. It’s actually an embedded language, means - it can be embedded inside HTML. WOW let’s see the above PHP code will work in a simple HTML file -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Class 1</title>
</head>
<body>
    <?php
        echo "Hello World. Welcome to PHP";
    ?>
</body>
</html>

Run on Browser

Now, in browser, reload again - http://localhost/phpclass/class1.php

And you’ll see this text in the browser -

Hello World. Welcome to PHP

Great, so we’ve seen that we can inject PHP inside of an HTML body tag.

Previous
Install PHP or Xampp in Windows / Linux / Mac - Easy PHP Server setup
Next
Variables in PHP - How to Create, Rules and Examples