# PHP: Numbers, Strings and Functions

September 19, 2018

Let’s write some code!

To start playing with PHP, we need to create a PHP file i.e. save a file with an extension of .php. Before that, do the following:-

• Make sure your server running. We covered Running the Server in the installation tutorial.
• Open the htdocs folder located in {your_xampp_directory}. By default, {your_xampp_directory} is located in C:\xampp\ in Windows, /opt/lampp/ in Linux and /192.168.64.2/lampp/ in OS X.
• Create a new folder inside {your_xampp_directory}/htdocs and call it basics.

Earlier, we picked out a code editor from the Code Editor section. We’ll need to open the editor now and write some code into a new file:

<?php
echo "Hello World!";
?>

Let’s have a look at what is going on here. A PHP script starts with <?php and ends with ?>. Here, echo is a PHP instruction that tells PHP to output the upcoming text.

The PHP software processes the PHP statement and outputs the text to the web browser. So after we run this code, we should expect Hello World! to be displayed on our web browser.

Save the file as index.php in the folder you just created: /{your_xampp_directory}/htdocs/basics/.

Open a browser and type in localhost/basics/ and press enter (If you’re on Mac, replace localhost with 192.168.64.2 or the ip address you had during installation). You should see something like this:

Nice! You just wrote your first PHP script.

Every PHP project you will be creating should be located in the htdocs folder present in {your_xampp_directory}. When we type in localhost/ in the browser, we are referring to the htdocs folder.

The index.php file is the entry point of any website/application. When you type in localhost/basics/ in your browser, then localhost/basics/index.php is loaded.

# What is a PHP File

A PHP file normally contains HTML tags, and some PHP scripting code.

<!DOCTYPE html>
<html>
<title>PHP Basics</title>
<body>
<h1>My First PHP Page</h1>
<?php
echo "<p>Hello World</p>";
?>
</body>
</html>

Notice how we can echo a paragraph tag <p></p> inside PHP! Save this file and refresh your browser. Right click on the page and click on View page source. You will see this:

You only see plain HTML! Where did our PHP code go?

The PHP code within the webpage is processed (parsed) by a PHP engine on the web server (Apache, in this case), which generates HTML.

So in the end, whatever PHP code you type in, produces dynamic HTML!

As a very brief introduction to HTML for absolute beginners,

• The body section is where all the content will go for your website.
• The head section has the title and details about javascript/css files that you wish to include.

We added our PHP script inside the <body> tag and that’s why we see the output in the browser as plain HTML.

Note You should notice one of the coolest thing about code editors: colors! Now you should see that the echo function is a different color from the text. This is called “syntax highlighting”, and it’s a really useful feature when coding. The color of things will give you hints, such as unclosed strings or a typo in a keyword name. This is one of the reasons we use a code editor. :)

# Numbers and simple math

Let’s start by typing some math, like 2 + 3.

<?php
echo 2 + 3;
?>

This will print 5. See how the answer popped out? PHP knows math! You could try out these too:

• echo 4 * 5;
• echo 5 - 1;
• echo 40 / 2;

Have fun with this for a little while and then get back here. :)

There are some more operators in PHP, but we won’t go over them right now. At the end of the article, there is a table with a list of all arithmetic operators in PHP.

# Expressions

Each line that you typed in above is called an expression. Expressions can be much longer too. For example, try this:

echo "<h1>";
echo (5 + 2) * (7 - 1) / 3;
echo "</h1>";

This prints out 14 enclosed within a <h1> tag.

In PHP(and other programming languages as well), all expressions evaluate to a single value. You saw this above for a simple expression such as 2 + 3 which evaluates to 5 and prints it to the web page.

Here’s a step-by-step breakdown of how the above expressions is evaluated.

(5 + 2) * (7 - 1) / 3
= (7) * (7 - 1) / 3
= 7 * (7 - 1) / 3
= 7 * (6) / 3
= 7 * 6 / 3
= 42 / 3
= 14

PHP does the above behind the scenes, and tells us what the final result of evaluating the expression by outputting HTML. This is what we mean by dynamic HTML.

As you can see, PHP is a great calculator. If you’re wondering what else you can do…

# Variable

In addition to numbers, we can use variables to store values.

Think of a variable as a container which can store some data. In PHP, a variable starts with the $ sign, followed by the name of the variable: <?php$x = 5;
echo "<h3>";
echo $x; echo "</h3>"; ?> Try this out. The output will be wrapped within a <h3> tag. This prints out 5 enclosed within a <h3> tag. That is, <h3>5</h3>. Here 5 is an integer. A variable can contain different types of data and we will look at all of them in detail in this course. One of the most common data types (in any programming language) is a String. # Strings How about your name? Type your first name in quotes like this: <?php$name = "Commonlounge";
echo "<h3>" . $name . "</h3>"; ?> You’ve now created your first string! It’s a sequence of characters that can be processed by a computer. This may be declared with single (') or double (") quotes (there is no difference!) The quotes tell PHP that what’s inside of them is a string. If you need to put an apostrophe inside your string, you have two ways to do it. Using double quotes: $y = "Runnin' down the hill";

or escaping the apostrophe with a backslash (\):

$y = 'Runnin\' down the hill'; Strings can be strung together. This is what is known as concatenation. echo "<h3>" .$name . "</h3>";

In the code above, you see how we used the . operator to concatenate strings. The above expression evaluates to:

"<h3>" . $name . "</h3>" "<h3>" . "Commonlounge" . "</h3>" "<h3>Commonlounge</h3>" Nice, huh? To get the number of characters in your string, simply type: echo strlen("Commonlounge"); // outputs 12 You just used the strlen function applied to your string! A function (like strlen()) is a sequence of instructions that PHP has to perform on a given object ("Commonlounge") once you call it. Tip: You can always add HTML tags to the echo statement using concatenation like so: echo "<p>" . strlen("Commonlounge") . "</p>"; If you want to know the number of words contained in a string, there is a function for that too! echo str_word_count("Hello world!"); // outputs 2 We’ll be learning more about PHP Strings and PHP Functions later in the course. For now, the above will suffice. # Comment A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code. <?php //this is a single-line comment /* this is a multiple-line comment */ #this is again a single-line comment ?> # Summary OK, so far you’ve learned about: • .php file – you wrote your first program in PHP and executed it on your web browser and understood how a .php file produces dynamic HTML! • expressions – you can now use PHP as a calculator to do arithmetic operations. • variable – it is a container to store data. In PHP, a variable is defined using the $ sign followed by the name.
• numbers and strings – in PHP numbers are used for math and strings for text objects
• functions – like strlen() and str_word_count(), perform actions on strings.
• comment – how to write single-line and multi-line comments in PHP.

These are the basics of every programming language you learn. Ready for something harder? We bet you are!

# Appendix: List of Arithmetic Operations

The following is a list of all arithmetic operations supported by PHP.

Operator | Example | Result | Description
---------|---------|--------|------------------------------------------------
+     | 4 + 5   | 9      | addition
-     | 4 - 5   | -1     | subtraction
*     | 4 * 5   | 20     | multiplication
/     | 8 / 3   | 2.666  | division
%     | 14 % 3  | 2      | modulo (remainder left after division)
**    | 5 ** 2  | 25     | exponent