echo in PHP

echo is using for output one or more strings in PHP.

echo is basically a language construct and not a function. So we can use without parentheses with it.

We can run shell commands also using shell_exec in PHP echo syntax.But its only used in advanced PHP programming and we will discuss more about this in our later posts.

We can use the HTML formatting tags with PHP echo statement

See the following sample program with echo in PHP programs

<?php
echo "Good Morning";
?>

Its out put will be :
Good morning

<?php
echo "<b>Good Morning</b>";
?>

Its out put will be :
Good morning

<?php
$variable1="PHP";
$variable2="MySQL";
echo $variable1."&".$variable2;
?>

Its out put will be :
PHP&MySQL

<?php
$variable1="PHP";
$variable2="MySQL";
echo $variable1."<br />".$variable2;
?>

Its out put will be :
PHP
MySQL

strlen() – PHP string length function

While our PHP programming , in some cases we need to get the length of a string for processing. strlen() if the function in PHP to get the length of a string.

See the sample program with strlen() function:

<?php
$variable="Check length";
echo strlen($variable);
?>

It will output the result

12

strlen function will count the blank spaces also contains in the string.

For looping through a string, we can use this function to know the end of loop by storing the length of string to a variable. And in other cases we can use this function for data checking like password length and all.

It will return 0 ( zero ) if the string is empty.

Concatenate strings in PHP using concatenation operator

Concatenation is the process of joining multiple strings into one.In PHP concatenate  strings is doing by concatenation operator ( “.” dot symbol ).Using of concatenation is highly used one in PHP programs. We need to use this for string concatenations and for printing the results in desired order etc.

Here is one Sample PHP program with Concatenate strings  using concatenation operator

<?php
$variable_1 =  "Syam";
$variable_2 = "is a PHP professional";
$variable_3 = "From India";
echo $variable_1." ".$variable_2." ".$variable_3;
?>

Output of this PHP program will look like
Syam is a PHP professional From India

In Some other cases we need to store the concatenated strings as a variable

See the following code

<?php
$variable_1 =  "Syam";
$variable_2 = "is a PHP professional";
$variable_3 = "From India";
$variable_4 = $variable_1." ".$variable_2." ".$variable_3;
echo $variable_4;
?>

In this code concatenated string is stored in the variable “$variable_4”  for later use in the program. Above code will ouput the same result as our previous example.

Variable Declarations in PHP

Variables are used for storing the data like strings, numbers , arrays etc in programs.

In PHP a variable starts with a $ sign. Forgetting to add $ sign is a very common mistake for the beginners.

Example for a PHP variable : $variable

And the declaration will look like  $variable = VALUE ;

A PHP variable name can start with a letter or underscore ( _ ) symbol and it shouldn’t contain any space or other special characters like *, % , # etc.

See the valid and invalid variable names

  • $employee name   ( Invalid variable )
  • $employee_name ( Valid variable )

There is no type declaration is needed in PHP variable declaration.variable will automatically converted to the type of assigned value to the variable.

For example :

  • $variable = 10 ; ( Here $variable will treat as integer automatically, we dont need to specify type as Integer at the declaration command )
  • $variable = “I love you”; ( Here $variable will treat as a string automatically, we dont need to specify type as string at the declaration command )

PHP Sample program with variable declaration

<?php
$variable_age = 30;
$variable_name= "Syam";
$variable_desp="has aged";
echo $variable_name."  ". $variable_desp."  ".$variable_age;
?>

The result will be look ke

Syam has aged 30

How a PHP program look like

PHP programs are saved in a file with extension “.php”  ( we can use it in .html files also , that I will explain other posts ).

PHP scripts will be in between <?php and   ?> tags.

<?php  is the starting tag ( where our php program in the page starts ) and ?> ending tag ( where our php program ends ).

PHP programs are embedding in between html codes and we can use the html codes inside our php programs also.

Basically a PHP program page will look like

<html>
<body>
<h1>How a PHP program look like<h1>
<?php
/* Here is a block of PHP codes in page */
?>
<h2> HTML portions </h2>
<?php
/* Here is another block of PHP code /*
?>
</body>
<html>

Here we can see that we can use any number of blocks of PHP codes with a mixture of HTML codes  in a page with starting and ending tags of PHP.

What is PHP and Full form of PHP ?

Full form of PHP is Hypertext Preprocessor

PHP is the most popular and widely used server side scripting language for web development. It is used to make the Dynamic pages in websites.

Rasmus Lerdorf was the creator of PHP in 1995.

PHP codes are embedding in HTML source codes for making the page dynamic. PHP can deal with most of the requirements in web development like Database, File handling, String operations, Arrays, Graphics, File Uploads, Data processing etc.

PHP can be used in any operating system with a web server Supports PHP. Apache web server is one of the popular web server dealing with PHP + MySQL. Moreover PHP is absolutely free to use.

Share
Share