• Welcome to our community, we're glad you're here. Please be sure to read our rules.

Introduction to PHP

Cosmic0355

New member
Hello!

I wanted to provide a quick introduction to PHP. PHP is a great language to learn. It's used by WordPress, the most popular CMS in the world, Facebook, and lots of others. PHP is in the "C" family of languages - it's a lot like C# and Java if you're familiar with those.

You start and end PHP with PHP tags. These are:

PHP:
<?php
    
?>

However if you have a pure PHP file (PHP only), it's considered best practice to leave off the closing PHP tag.

Let's write our first PHP code!

PHP:
<?php
echo "Hello, world!";

If you save this as helloworld.php and run it, you should see:

Hello, world!

in your browser.

echo tells PHP to print out what is in the quotes. This is useful for printing out HTML or showing what a variable contains. Consider the following code:
PHP:
<?php
    $variable1 = "Hello";
    $variable2 = "World";

    echo $variable1;
    echo $variable2;

What do you think it will output?

HelloWorld
Notice how the two strings are on the same line despite echo being on different lines. If you want to insert a line break, you either need to use HTML or if you're writing a CLI program, a new line character: \n.

Let's modify our program a little bit.

PHP:
<?php
    $variable1 = "Hello";
    $variable2 = "World";

    echo "$variable1 <br>";
    echo $variable2;

This will give us:
Hello
World
Pretty easy, right?

Let's mix it up a little bit:
PHP:
<?php
    $variable1 = "Hello, World!";
    $variable2 = "Jack";
    $variable3 = 37;

    echo $variable1;
    echo "\n$variable2 is my name! Don't wear it out!";
    echo " I'm " . $variable3 . " years old!";

This will produce:
Hello, World!
Jack is my name! Don't wear it out! I'm 37 years old!
Easy! :D

I'll be back with more!
 
Top