What is PHP
PHP (recursive acronym
for PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose
scripting language. PHP is geared towards web development. It was originally
created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in
1995. PHP is the most popular open-source server-side scripting language that
many developers (devs) use for web development.
PHP code are executed by
a part of PHP called the PHP run time. The PHP run time looks for two different
things within a web browser before it can identify that the code written are
PHP code. They are the file extension: for the PHP run time to know that the
webpage will contain a PHP code, save the file name with a .php extension.
Secondly, the code MUST be writing within the PHP code block. It is
important to note that PHP help us to develop a dynamic website.
How to Write PHP Code
Before writing your PHP code you need to first have your XAMPP installed to your local computer, you can use Notepad or any other text editor to write your source code. Example of text editor are Sublime, Visual studio code etc. Open your text editor and write your source code save the file with a name that describe what the source code dose (in our case welcomeMsg.php) and save it with a .php file extension.
Every PHP code is written
within the PHP tag which is also called the PHP code block (<?php ?>). Writing a PHP code
outside the PHP code block will not lead to an error but the web browser will
interpret the code as a normal text and render
it as u write it on the web browser. Echo is an output function outputting the
string Welcome to EasyCode blog. A string can be written either
within a single quote or a double quote but don’t mix it up (Our example uses a single quote). Always end a PHP
code with a semi colon to avoid syntax error. The semi colon indicate the end of line and not doing
so will lead to a syntax error at run time.
How to Display our Output on Web-browser
Open your favorite web browser and type the following localhost/file-directory/webpage-name.php. The file directory name should be replace by any name the developer use to create his or her folder in the htdocs folder in our case phpTutorial. The webpage name is the name of the webpage you want to redirect your users in our case hello.php. So we will be typing localhost/phpTutorial/hello.php
Output of The Code Above
Welcome To Easycode Blog
Inspecting PHP Source Code Via Web Browser
Developers and users can view source code from the developer console. The developers console provide a means for accessing all of the information that has been sent to your machine - from fragments to files. The developers console is largely meant to be used as a tool for developers - primarily for debugging their source code. You can access the console by following the steps below.
Open your web browser, right click and select inspect code, what will be displaying will be just the code and not all your source code. This is because PHP is a server side programming language.
PHP and White Space
White space means nothing
to PHP, the white space will not be rendered on the web browser or when you are
inspecting your code, but it is often use because it help format your code and gives
it a better look.
How to Add HTML Code to PHP Script
To accomplish this task
you have to place your single quote and double quote properly, example if your
echo function has double code, then the code within you HTML should be single
quote and vise versa.
Example