PHP String

A string is a text that might contain characters, numbers or any special character. First of all, we will create a string. Either a string can be stored in a string variable or it can be directly output to a browser without using any variable. Consider the php example:

In the above example, we have used a string variable named, “str” that contains the string, “String variable is being used”. The first echo statement will display the string, “String variable is being used” and the second echo statement will display the string, “Direct output”.

The string is enclosed in double quotes. You can also use single quotes. Suppose you want to display an apostrophe in a string. Let’s see how you will do this.

<?php
echo ‘It’s a very good site to learn PHP!’;
echo ‘It won’t let you down!’;
?>

Here we have used backslash() to escape the apostrophe. The output of the above PHP script in a web page will be:

It's a very good site to learn PHP! It won't let you down!

The output will appear in a single line because there was no <br> tag present. Let’s have a look at a few more escape sequences:

Character

Description

n

Will add a new line

t

Will add a tab

r

Carriage return

$

A dollar sign will be displayed

“

Double quotes are displayed

PHP String “heredoc”

Apart from the two ways to create a string that we discussed earlier, there is one more way to create a string-“heredoc”. It is used to create a multi-line string without the use of quotes. Have a look at the code given below:

Let’s have a look at this code. “str” is a string variable which stores multi-line string. It starts with <<< and an identifier. Here, we have used “TRY”. The “heredoc” ends with the identifier in the end followed by a semicolon. Remember that the closing sequence “TRY;” must be on a separate line that must not be indented. The output of the above script on a web page will again be in a single line due to the absence of <br> tag as shown below:

PHP comments

Comment is that part of the code that is not parsed by the parser. The main objective of the comment is to provide necessary notes to the programmer. It helps in understanding the code better. Every language has a different way of storing a comment. In PHP, the comments are stored in a manner similar to that of C++. Let’s see a single line comment in a PHP script.

Why is there only a single line in the output when we have given the echo command twice. The reason is that the first statement is a comment. Any statement written after double slash (//) will not be parsed by the parser and hence, you can put absolutely any notes there for your convenience. But now suppose, we want to add multiple lines of comment instead of a single line comment. It would be very tedious to put “//” at the beginning of each line. This problem has been simplified. Multiple line comments are handled in the following manner:

<?php /*This is a multiple line comment. You can as many lines as you want here. They will not affect the PHP script.*/ echo “We just learnt multiline comment.”; ?>

In the above code, we have used “/*” to mark the beginning of the comment and “*/” to mark the end of the comment. And here’s the output to the above script:

$a = 4;
$b = 2;
1) $a = $b; (assigns a the value 4)
2) $a+=$b; (a will have the value 6)
3) $a-=$b; (a will have the value 2)
4) $a*=$b; (a will have the value 8)
5) $a/=$b; (a will have the value 2)
6) $a%=$b; (a will have the value 0)

Comparison Operators

Operator

Description

==

$a==$b; will return true if value of a is equal to value of b

!=

$a!=$b; will return true if value of a is not equal to value of b

>

$a>$b; will return true if value of a is greater than value of b

<

$a<$b; will return true if value of a is less than value of b

>=

$a>=$b; will return true if value of a is greater than or equal to value of b

<=

$a<=$b; will return true if value of a is less than or equal to value of b

Below are a few examples:

$a == $b;// test if two values are equal
$a != $b;// test if two values are not equal
$a &lt; $b;// test if the first value is less than the second
$a &gt; $b;// test if the first value is greater than the second
$a &lt;= $b;// test if the first value is less than or equal to the second
$a &gt;= $b;// test if the first value is greater than or equal to the second

PHP Variables
What are variables?
Variables are names that store values like strings or numbers. Their values can change at any point in the whole program. Since the value is variable, they are known as variables.

Declaring Variables
Most programming languages require variables to be declared. This is done to ensure what data type it is and hence, what type of data it can store. Each programming language has its own set of data types. In case of PHP, the variables do not have to be declared. PHP automatically converts the variable to the correct data type, depending on how they are set. Hence, PHP is known as a loosely typed language. A strongly typed language requires you to declare the variables in the beginning of the program. Assigning values to variables in PHP
Variable names begin with a dollar sign ($). Lets have a look at the following statements:

* A variable name must start with a letter or an underscore (_).
* A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ).
* A variable name should not contain spaces. If needed, underscores should be used.
* Variabe names are case sensitive.
* String constants are enclosed in quotes (“).
* Numeric values are assigned as it is.

PHP Output

To display the result of a PHP script, there are two basic commands. They are echo and print. Both these commands are used in PHP to display any text. The only difference between these two commands is that print() function is slower than echo() function. Hence, echo has an upper hand and is used more. Let’s understand both these functions with the help of examples.
echo function
Let’s see an example below to display the text, “My first PHP program!”.

<html>
<body>
<?php
echo “My first PHP program!”;
?>
</body>
</html>

The output will be:

My first PHP program!

print function
The print function is used in a manner similar to echo statement.

PHP Basics

PHP runs on different platforms.

PHP is compatible with almost all servers used today.

PHP is FREE to download from the official PHP resource.

How to save PHP files?

PHP files have the extention .php. PHP files are not different from HTML files. It is basically a simple HTML or any other SGML file having some php script in between. No matter where it is placed, PHP code is executed before any of the HTML code. The only difference is that the file must have the extention .php, else the parser will not parse it.

PHP syntax

A PHP block can occur anywhere in any SGML file. The syntax for the same is given below:

<?php
PHP script here
?>

Some servers have shorthand support and the syntax for PHP in such cases is:

<?
PHP script here
?>

But it’s always recommended to avoid the shorthand way and use the former way.
Have a look at a simple PHP script given below:

<html>
<body><?php
echo "My first PHP program";
?></body>
</html>

The above example will send the text “My first PHP program” to the browser. One thing to note is that every statement in the PHP code must have a semi-colon in the end. “echo” is a statement that outputs the text to the browser.