Diving Deeper into PHP – Part III

Till now, we have learned quite a bit about PHP and have also created out first PHP file. Now, it’s time for us to dive deeper into PHP and learn some more interesting stuffs. In this part of the course, we will be learning about php.ini configuration file, phpinfo() function, outputting HTML using PHP and also about using variables in PHP.

You will possibly never lose the interest reading this article, as there are a variety of things for you to learn this time. So sit back, relax and just give your full attention to this article.

The Importance of php.ini File and phpinfo() Function

php.ini – The PHP Configuration File

The “php.ini” file is nothing but a simple configuration file which is read and executed just at the time when PHP starts. In simple words, this file contains all the options for you to fully customize the way your PHP installation works, where the “.ini” extension suggests that that file represents configuration file.

php-ini-file

You can find this file located inside the “php” folder which is inside your “xampp” installation package. For my installation, the location of this file is, C:/xampp/php/php.ini. Let us open this file in ConText to view the details of it.

php-ini-file-details

The first thing you would notice after opening this file is that there are a lot of lines which are starting with a semi-colon at the beginning. The semi-colon works in turning these lines into simple comments so that these lines do not get processed at all when this file gets executed. The best thing about PHP is that it is very well-documented and the php.ini file is also not an exception to this.

You will notice that almost each and every setting in this file have got a brief description of what it actually means and does with your PHP installation. Though it seems to be a very large file with thousands of options, but it actually isn’t as it is mostly covered with descriptions for each of them.

It is very important to know about the configuration of your PHP installation right at the beginning. We may not need editing this file right now, but we will surely need to make some important changes in these settings very soon.

phpinfo() – Outputting the detailed PHP Configuration

phpinfo() is a simple yet a very important function which you can invoke in your PHP scripts to output a very detailed configuration information of your entire PHP installation. Each and every installation is different in its own way, and this function can be used to get almost every possible information you may need about a particular PHP installation.

[php] <?php

phpinfo();

?> [/php]

But there is something that you need to be cautious about while using this function. You should never use this function in any of your PHP files while the site is actively running and people are visiting your site. This file contains a lot of sensitive data and if this data falls into the wrong hands then the security of your PHP application or site can get compromised easily.

So, just use this function when absolutely necessary and delete it after use. Never keep this function running in any active PHP file. In the screenshot below, I am showing you this function in action, as it’s in my local server and nobody can harm my applications by getting hold of some sensitive data.

php-info-function

Outputting HTML using PHP

Previously, we have learned quite in-depth about outputting data in our PHP file using “echo” and “print” statements. But now it’s time to go beyond that by outputting HTML using PHP. It’s really very simple and works something like this –

[php]<?php

echo “<center><b>PHP for Beginners Couse</b></center>”;

?>[/php]

html-using-php-1

Now, you can see that the text comes out in bold and is aligned to the center of the page. This shows us that the HTML output works perfectly for our PHP file. You could have also used single-quotation marks instead of double-quotation marks and that would have also given you the exactly same output. So, this leads to an ambiguous situation which we must discuss right now.

Now, let us try with another example which will lead to an error and that is exactly what we want,

[php]<?php

echo “<center><font size=”10” color=”red”>PHP for Beginners Course</font></center>”;

?>[/php]

html-using-php-2

Now, the error tells us the interpreter expects a “;” instead of “10” just after the double-quotation marks. The reason behind this error is that the starting quotation marks for the “echo” is ending just before “10”.

So here comes a problem with the use of quotation marks (we have also talked about in the first part of the series) which needs to be discussed in detail right now. You need to learn the proper way to use quotation marks (single or double). There are essentially three ways to properly deal with this,

i)    Firstly, what we can do is simply change all the inner double-quotation marks into single quotation marks.

[php]<?php

echo "<center><font size=’10’ color=’red’>PHP for Beginners Course</font></center>";

?>[/php]

 php-inner-single-quotation-marks

Now your code should run perfectly and the result will also be prefect as you had expected. But you won’t not be able to change the inner quotation marks always, so there should be a second way to deal with it.

ii)    We can also use a simple trick and solve this problem by using a “” (backslash) before each of the inner double-quotation marks.

[php]<?php

echo "<center><font size=”10” color=”red”>PHP for Beginners Course</font></center>";

?>[/php]

Now this backslash instructs the interpreter to skip the symbol following it and thus the script runs perfectly to give the exact result as we had expected. While this is an effective solution, but is tiresome and confusing sometimes.

iii)    So this paves way for the third solution which is as simple as changing the outer double-quotation marks into single-quotation marks and keeping everything else intact.

[php]<?php

echo ‘<center><font size=”10” color=”red”>PHP for Beginners Course</font></center>’;

?>[/php]

This code runs perfectly as expected and is also the most preferred solution of the three. Moreover, echo with single-quotation marks works much faster than with double-quotation marks.

So we have learned some of the ways of outputting HTML using PHP which we will be definitely requiring in many advanced PHP applications that we are going to development. Instead of echoing out simple HTML tags, we can also echo out an entire HTML document if we want to. Here is a typical example of it.

[php]<?php

echo ‘<html><head><title>Welcome to corePHP.com</title></head><body bgcolor="#E6E6E6"><center><font size="10" color="red">PHP for Beginners Course</font></center></body></html>’;

?>[/php]

In this example, everything is in a single line but it should look something like this –

[php]<?php

echo ‘<html>

<head>

<title>Hello Title!</title>

</head>

<body bgcolor="#E6E6E6"><center><font size="10" color="red">PHP for Beginners Course</font></center>

</body>

</html>’;

?>[/php]

Variables in PHP

Now it’s time for us to move into learning about “variables” in PHP. I think you should at least have a bit of idea of what variables actually mean from your elementary algebra knowledge, where we have used x,y,z variables to hold values like 10, 15.5, 30, etc.

In simple words, variables are just like containers which can hold a particular value which is liable to change at a later point of time. In PHP, a typical variable looks something like this,

[php]$name = “PHP”;[/php]

But there are some rules which you need to know while declaring variables in PHP,

i)  All variables must begin with the “$” sign,

ii)  Variables can begin with an alphabet or an underscore symbol,

iii)  Variables can never begin with a number,

iv)  Variable name can contain upper-case and lower-case alphabets, numbers and underscore,

v)  Variables are case-sensitive.

A typical PHP script which shows the use of different types of variables, but we will obviously learn in-depth about it in the later parts of the series.

[php]<?php

$name = “PHP”;

$Name1 = 100;

$Na_me_2 = 55.5;

$__name = true;

?>[/php]

This script runs perfectly as we have not broken any of the rules of variable declaration. An interesting thing to note here is that we didn’t mention the types of the any of the variables anywhere while declaring them in our script. If you are familiar with languages like C, C++ or Java, then you had always been declaring variables with their respective data types like an integer variable would always be declared in a way something like this,

[php]int x = 100;[/php]

PHP is a loosely-typed language and is a bit different from languages like C, C++ and Java (strongly-typed) where you do not need to worry about the data types of the variables. Here in PHP things are much more dynamic and flexible, as we can assign any type of data to any variable we want.

But every advantage have always got some kind of disadvantages too. The biggest disadvantage of this opportunity is that many programmers tend to write very poor and unorganized code, but this drawback can easily be overcome with a little bit of consistency. We will obviously be discussing more on strongly-typed and loosely-typed languages in the later parts of the course.

The Final Words

From this part of the course, we are heading towards becoming serious PHP developers. We have learned about the PHP configuration file, php.ini and about phpinfo() function which can be used to get a detailed information of any PHP installation.

We have also learned in detail about outputting HTML using PHP and the proper way of using double and single quotation marks. I consider these things to be some of the most important things that you need to know right from this moment, so that your basics or fundamentals remain very strong.

Finally, we have also learned about variables in PHP, the rules to declare then and the fact that PHP is a loosely-typed language with no importance in data types of the variables.

See you in the next part, which is going to be even more interesting with lots of important things to learn.

STAY UP TO DATE

Sign up today to stay informed with industry news & trends