PHP strlen

strlen()-To find the length of a string

To find the length of a string, we use a function strlen(). Let’s use this function to find the length of a string.

<?php  echo strlen("Learn PHP");  ?>

The output of the above code will be:

9

Alternatively, we can write the PHP script like the way below. The output will be the same as above.

<?php  $str="Learn PHP";  $length=strlen($str);  echo $length;  ?>

PHP Concat Strings

Concat Strings

Two strings can be concatenated by using the operator dot (.). Let’s see this with an example:

<?php $str1="Learn"; $str2="PHP"; echo $str1.$str2; ?>

The result for the above echo statement will be:

LearnPHP

Now look at the script given below:

<?php $str1="Learn"; $str2="PHP"; echo $str1." ".$str2; ?>

The result of this script will be:

Learn PHP

Here, we used the concatenation operator twice to include another string i.e. a blank.

PHP Strings

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:

<html> <body><?php $str="String variable is being used"; echo $str; echo "Direct output"; ?></body> </html>

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:

<html> <body><?php $str=<<<TRY This is an example to demonstrate heredoc! TRY; echo $str; ?></body> </html>

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:

This is an example to demonstrate heredoc!

PHP Comments

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.

<?php //echo “We are checking a comment.”; echo “We are checking a comment.”; ?>

The output will be:

We are checking a comment.

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:

We just learnt multiline comment.

PHP Operators

PHP Operators

The PHP operators can be divided broadly into 5 categories namely,

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators

Arithmetic Operators

Operator Description
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
Decrement

Let’s see a few examples.

$a = 4; $b = 2; $Add= $a + $b; $Sub= $a - $b; $Multip= $a * $b; $Div= $a / $b; $Mod= $a % $b; $Increm= $a++; $Decrem= $b--;

The reult of the above will be:

Add=6 Sub=2 Multip=8 Div=2 Mod=0 Increm=5 Decrem=1

Assignment Operators

Operator Description
= $a = $b; assigns a the value of b (Equal)
+= $a+=$b; means $a = $a+$b; (Addition)
-= $a-=$b; means $a = $a-$b; (Subtraction)
*= $a*=$b; means $a = $a*$b; (Multiplication)
/= $a/=$b; means $a = $a/$b; (Division)
.= $a.=$b; means $a = $a.$b; (Concatenation)
%= $a%=$b; means $a = $a%$b; (Modulus)

Let’s take a look at a few examples:

$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

Logical Operators

Operator Description
&& and; returns true when two or more conditions are be true
|| or; returns true when either of the two conditions is true
! not; negates a given condition

PHP Variables

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:

$worda = “gnulamp”;
$wordb = “php tutorial”;
$website_url = “http://www.gnulamp.com”;
$word=”$worda $wordb”;
$num1 = 10;
$num2 = 20;
$sum = $number1 + $number2;

Some rules to remember

* 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

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.

<html>
<body>
<?php
print (My first PHP program!);
print “My first PHP program!”;
?>
</body>
</html>

The output will be:

My first PHP program!
My first PHP program!

PHP Basics continued

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.

PHP Tutorial Basics

PHP is a recursive acronym for “PHP: Hypertext Preprocessor”.

  1. It is an open source.
  2. It is a server-side scripting language, i.e. the PHP script gets executed on the server.
  3. It is widely used in web development.
  4. It can be easily embedded into HTML.
  5. PHP is very much similar in syntax to C or Perl.
  6. How and where PHP is used
  7. Server-side scripting:PHP is mainly used to make web pages which are dynamic and interactive. There are three things needed for this purpose namely, web server, web browser and PHP parser.
  8. Command line scripting:This is used when you don’t want to use browser or server. Only PHP parser is needed. The script can be executed using cron on Linux or Task Scheduler on Windows.
  9. Desktop applications:Though not recommended, PHP can be used to write desktop applications using PHP-GTK. This is used specially in writing cross-platform applications.
  10. Some advantages of PHP
  11. PHP can be used on all major operating systems, including Linux, Unix.
  12. Most of the web servers can be used including Apache, Microsoft Internet Information Server.
  13. PHP can be used to create dynamic and interactive web pages.
  14. PHP code can be easily embedded into HTML code.
  15. PHP is free.
  16. Databases Supported by PHP
  17. Some databases that are supported by PHP are Adabas D, InterBase, PostgreSQL, dBase, FrontBase, SQLite, Empress, mSQL Solid, FilePro (read-only), Direct MS-SQL, Sybase, Hyperwave, MySQL, Velocis, IBM DB2, ODBC, Unix dbm, Informix, Oracle (OCI7 and OCI8), Ingres and Ovrimos.