PHP7 GUIDE FOR NEW PROGRAMMERS 2018 ECHO PRINT VARIABLES LESSON 3

PHP 7.2.11 is the latest PHP version. We will be using this version for this series of lessons. We will take you from an absolute beginner programmer with no knowledge of PHP and build upon that. Please make sure that you have completed the absolute beginner web development course. We will be building off of those lessons to help you gain an accurate knowledge of PHP and the way it works with HTML.

In the previous lessons we set up the XAMPP server. We tweaked it a little more in lesson #2. If you have completed those steps you are ready for this lesson.

PHP Extension

In order to make PHP work on your server you will have to create and name files with the .php extension. Apache will look for index.php in order to create an index file for your website. People generally refer to this at the home page when creating an index.php file at the root level. All files that you write php syntax in have to have the extension .php at the end of the file name. Here are some examples.

Only cool people share!

  • foo.php
  • index.php
  • doo.php

PHP Tag

A PHP tag is written like this. <?php ?>. In order to execute PHP everything will need to be written inside this tag.

PHP Echo Print

PHP uses echo and print in order to execute a string or an integer. Here is an example of a PHP syntax that will execute in a browser.

<?php echo 'Hello There'; ?>

When loaded in a browser the only thing that you will see is “Hello There”. The rest of the PHP syntax will not be read by the browser because the browser only reads HTML. The same would be true if we used the “print” command. Here is an example of using the PHP print command.

<?php print 'Hello There'; ?>

This too will only show “Hello There” in a browser.

What Is the Difference Between Echo and Print in PHP?

Using “Echo” is faster than using “print”.  “Echo” can also take more than one parameter than “print”. “Echo” holds a value of 0 while “print” holds a value of 1.

Echo Print Multiple Parameters

In this example we can use “echo” to echo out a multi parameter string.

<?php echo 'Hello', 'world'; ?>

This will work.

<?php print 'Hello', 'world'; ?>

This will not work.

In our lessons we will generally use “echo” instead of “print”.

PHP 7.2.11 Creating Strings

In the above examples we were creating PHP strings. A string is any data made up of numbers, letters and even symbols. Here are some PHP examples of strings.

<?php echo 'This is a php string'; ?>

<?php echo '<h1>This is also a php string with some html added</h1>'; ?

<?php echo '<p>This is a php string with a html and a style tag added</p><style>p{color:red}</style>'; ?>

 

PHP 7.2.11 Creating Variables

A variable in PHP starts with a dollar “$” sign. You can name a PHP variable whatever you want. The PHP variables can hold a string like in this example.

<?php $foo='This is a string stored in a variable called foo'; ?>

The variable will not echo itself on the page without an echo command. You have to make sure that you have the variable stored and then echo it out like in this example below.

<?php $foo='This is a string stored in a variable called foo';  echo $foo; ?>

More on PHP

I hoped you enjoyed this lesson in PHP. I am setting the groundwork to help you learn to program in PHP. In the next lesson we will assemble a simple webpage all written in PHP. Please like, share and subscribe for the next update.

PHP7 GUIDE FOR NEW PROGRAMMERS 2018 ECHO PRINT VARIABLES LESSON 3 was last modified: October 29th, 2021 by Maximus Mccullough
PHP7 GUIDE FOR NEW PROGRAMMERS 2018 ECHO PRINT VARIABLES

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.