Introduction To PHP
First off remember that PHP pages are not limited to purely writing PHP code. The PHP code is only inside of the <?php
and ?>
tags. Anything outside of those tags would not be PHP and could be anything that you could normally write in an HTML file. With a standard PHP file that is not going to be used as an include, you will have html tags in the file.
I am sure you know something about file types if you have made it this far. Simply put, make sure your files have the .php extension or else the server won't process the code. If PHP is your first language other than HTML, CSS, and JavaScript, you will realize that PHP isn't nearly as forgiving. Be consistent and use the proper syntax. Enough chatter, let's write our first PHP script!
Example
<html>
<body>
<h3>My First PHP Code</h3>
<?php
echo "I am awesome!";
?>
</body>
</html>
Result
My First PHP Code
I am awesome!The command echo
is one of the ways PHP can output content. So, basically we used the opening <?php
tag to indicate that were starting to use PHP code. Then, we echo a string "I am awesome!", which you are. And of course, we end our only statement with a standard ;
. Finally, we tell the server that we no longer want to use PHP code and we close it with the ?>
tag.
If you have this idea mastered, continue on to learn about the fundamental stones to craft a master PHP website.