Adam Roynon
Posted on February 22, 2020
PHP is a server-side scripting language used by web developers to create functionality and processes that run on the server in response to a web request. PHP is a recursive acronym that stands for PHP Hypertext Preprocessor. You can use PHP to respond to a request from a webpage or in conjunction with AJAX to create real-time request and response procedures.
Imagine you have a webpage that you want to display the correct time to the user. You could write some PHP directly in the page so that when the page is loaded the correct time is displayed or you could have a REST endpoint created using PHP on the server-side to respond with the current time and then use a client-side scripting language, such as JavaScript, to run an AJAX request and update the time on the webpage. Real-time requests using AJAX are often used within social media sites when updating the subscription feed without a page reload, such as a scroll down to refresh feature on most mobile social media applications.
When writing a PHP file the extension should be '.php' and the code must be encased within PHP tags. The opening PHP tag is '<?php' and the closing tag is '?>', all of your PHP code must be contained within these tags. You can create standalone PHP files to handle requests or for processes that are done entirely on the server-side, or even to structure your server-side data model.
<?php
echo "Hello World";
?>
However, you can include PHP directly within an HTML page but you have to change the extension from '.html' to '.php' if the page contains PHP that you want to be executed. Also, when using a server-side scripting language such as PHP the website must be hosted on a web server, even a local webserver, as the webserver is what executes the PHP code and not the client as with client-side languages such as HTML or JavaScript. As you can see the PHP within the HTML document is still contained within PHP opening and closing tags. However, when the user requests and loads this page they won't see any of the PHP code as it will have been executed and removed from the final rendered page.
<body>
<?php
echo "Hello World";
?>
</body>
This post was originally published on https://acroynon.com
Posted on February 22, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.