PHP introduction

PHP is an acronym for "PHP: Hypertext Preprocessor". It is a powerful server side script for creating dynamic and interactive Web pages. PHP files can contain text, HTML, CSS, JavaScript, and PHP code. PHP code are executed on the server and the result is returned to the browser as plain HTML. PHP files have extension .php

PHP can generate dynamic page content. It can create, open, read, write, delete and close files on the server. PHP can collect form data, send and receive cookies as well as add, delete or modify data in your database. PHP can be used to control user-access or encrypt data. With PHP you are not limited to output HTML. You can output images, PDF files and many more. You can also output any text such as XHTML and XML.

Basic Requirements to Work on PHP

To start using PHP, you need to install a virtual web server XAMPP on your own PC. The project directory must be inside the htdocs folder of XAMPP to be loaded in browser. The files must have the exension .php

HTML markups are well supported by php. Infact, also generates html codes into html markups. Php files can't be opened in browser directly like html files. Instead, you need to open the browser and call the local server in url followed by your project directly. If your apache server is running on port 80, then you can type

localhost/yourProjectDirectoryName/yourProjectFileName/

Else, you need to call the port number as well.

localhost:portNumber/yourProjectDirectoryName/yourProjectFileName/

In order to open the php files, you need to initiate the XAMPP application and start the apache server. While you are working with database, you need to start the MySQL as well to connect to thedatabase.

Syntax

While working with css in HTML files, we need to wrap css codes with <style> </style> and <script> </script> for js codes, php script starts with <?php and ends with ?>. <? ?> is also supported but needs a bit of configuration in apache server and won't support in other computers if the configuration is not modified in that one. If no html markups are present in the page closing php tag ?> is not needed.

Printing on PHP

echo is the command to print characters in php.

<?php
echo "My first PHP script!";
?>   

Result

My first PHP script!

We can't print the characters directly with php echo command. The characters need to be wrapped up with double quotes to make it string and the command must end with a semicolon;

Let's see a php function too then.

Function is very important in php. All functions have their own specific tasks. As we progress to objected oriented php, all the php codes are executed in functions to keep the view files clean and the project documents well organized. php functions don't need double quotes.

echo phpversion();

It's an inbuilt function. This phpversion() function displays the php version being run on the computer.

php_info();

This function doesn't require echo command and provides extended details about the php version, operating system, server, configuration settings, environment settings, upload limits, and much more. Execution of any of these php commands also indicates that php is successfully running in our computer.

Echo vs Print

echo ('hello world');
print ('hello world');

Both print functions are valid. In order to keep consistency in script, it's better to choose only one of them across a project. Echo has a merit over print though as echo allows us to print multiple strings with a single command while print doesn't. So, echo command is preferred by developers round the world.

supported - echo ('hello','world','check');

unsupported - print ('hello','world','check');

var_dump('hello');

It's a debugging tool for developers. It allows us to check the type and value of data before adding more executable functions related to the data.

print_r();

it's also a debugging tool that displays array data. These tools are very important and are used extensively in the development environment.

Comments

PHP supports several ways of commenting.

	<?php
// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/

// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>  

Case Sensitivity

In PHP, all keywords such as if, else, while, echo, etc. along with classes, functions and user-defined functions are NOT case-sensitive but all variable names are case-sensitive.

<?php
$color = "red";
Echo "My car is " . $color . "
"; echO "My house is " . $COLOR . "
"; ?>

In the above example, Echo and echO keywords work the same but the variables color and COLOR are seen as different values.


0 Like 0 Dislike 0 Comment Share

Leave a comment