PHP Variable Interpolation

PHP supports interpolation of variables in between set of words to be printed to the browser using PHP output statements like echo(), print() and etc. There are two methods in variable interpolation. First method is to use variables in between string enclosed by double quotes which is shown as follows.

$name = "wearecoders";
echo "I am reading $name"; // output: I am reading wearecoders

Then, the variable interpolation can also be implemented with here document which contains several lines of string to be printed. PHP variables will be included to replace their value. A here document will be as follows to print the content as it is including line breaks, quotes and etc.

$myDoc = <<< EOD
I am reading wearecoders
to know all about PHP
EOD;
echo $myDoc;

In another method of variable interpolation, the variables will be enclosed by curly braces to replace its value inside a word that is not separated by any white space. This can be clearly understood by the following example.

$name = "PHP";
echo "I am reading {$name}wearecoders"; // output: I am reading PHPwearecoders

Here, in the above program, the variable $name is associated with the string wearecoders without any white space. So that, the curly braces are needed around the variable name to distinguish both. Otherwise, execution process will look for the variable $namewearecoders, which is no more.

Note:

  • Among both of the above two methods, the first one is the easiest method.
  • Variable interpolation is not possible with in single quotes

PHP Line Breaks

Line breaks are the separators which are used to escape from continuing with the same line. This is for splitting lengthy line into small, easy readable chunks to be shown line by line. PHP allow to add these line breaks by using escape sequences or predefined constants, as listed below.

  1. Linefeed (\n): This is one of the PHP escape sequence to add actual line break in between content.
  2. PHP_EOL: This Predefined constant is used as an representation of end of line.

Note:  These two PHP escape symbols will be effective only within double quotes. If we have content that is enclosed by single quotes, then, we should specify the line breaks by pressing the enter key as usual. For example,

$content = 'PHP line breaks
added by escape sequence
working fine';

But, with in single quotes we can only use static data. Rather, if we we want to interpolate PHP variable dynamically, then we need to use double quotes.

The following program shows about how to use PHP line feed and PHP_EOL for adding line breaks for the same content displayed within single quotes.

$content = "PHP line breaks\nadded by escape sequence\nworking fine";
//OR
$content = "PHP line breaks" . PHP_EOL . "added by escape sequence" . PHP_EOL . "working fine";
echo $content;

In the above program, we can note that the \n is used without any PHP dot (.) separator where as it is required, while using PHP_EOL as a line break to recognize that it is PHP predefined constant.

Purpose of PHP function nl2br()

When we execute the above program, we can not see the effect of added line breaks in browser for each new line. But it could be seen, when we write the content into a file or to the database, since these are actual line breaks.

If we want to see the line breaks with browser output as they are in file or database, then we need to convert this actual PHP line breaks into HTML line breaks which are provided with the stand alone <br/> tags. For this conversion, PHP provides in built function named as nl2br(), that is, converting new line into break.

This function required an argument for getting input with which it will search for new line to be replaced with HTML line breaks. For example, the $content variable that holds the data with actual line breaks will be passed to this function as follows.

$content = "PHP line breaks\nadded by escape sequence\nworking fine";
//OR
$content = "PHP line breaks" . PHP_EOL . "added by escape sequence" . PHP_EOL . "working fine";
$content = nl2br($content);
echo $content;

As shown in the above code, before printing $content to the browser, it is applied as an argument of nl2br() to make the line breaks visible with browser output.

Auto suggestion using Ajax, jQuery and PHP

Must Read : Auto suggestion using Ajax, jQuery and PHP

Step 1: Creating database table for auto suggestion application
CREATE TABLE IF NOT EXISTS autosuggession (
user_id int(11) NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL,
PRIMARY KEY (user_id )
);

Now we insert some row in our table
INSERT INTO autosuggession (user_id, username) VALUES (NULL, ‘De Gea’), (NULL, ‘Rafael’), (NULL, ‘Shaw ‘), (NULL, ‘Jones’), (NULL, ‘Rojo’), (NULL, ‘Evans’), (NULL, ‘Di Maria’), (NULL, ‘Mata’), (NULL, ‘Falcao’), (NULL, ‘Rooney’), (NULL, ‘Januzaj’), (NULL, ‘Smalling’), (NULL, ‘Lindegaard’), (NULL, ‘Hernandez’), (NULL, ‘Carrick’), (NULL, ‘Herrera’), (NULL, ‘Henriquez’), (NULL, ‘Valencia’);

Step2: Creating view file index.php for auto suggestion(You can give any name)

Step 3: Create style.css

Step 4: Now create autosuggestion.js file

Step 5: Create search.php for ajax request

 

Username availability checking using Ajax

Must Read : Username availability checking using Ajax

Step 1: Creating database table for username availability checking and insert some data
CREATE TABLE IF NOT EXISTS user_information (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
email varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY email (email)
)

INSERT INTO user_information (id, username, email, password) VALUES
(1, ‘admin’, ‘admin@admin.com’, ‘12345’),
(2, ‘admin123’, ‘admin123@admin.com’, ‘12345’),
(3, ‘wearecoders’, ‘info@wearecoders.net’, ‘12345’);

Step2: Now we Creating our view file index.php. This file will show a html form.

Step 3: Create style.css for styling our view file

Step 4: Now create usernamevalid.js file. This file will take value from the form and using ajax we show the availability message to the user.

Step 5: Create the connection file conn.php to create php with mysql

Step 6: Create user_valid.php for username availability checking. This file receive the ajax post request for username and check if the username already exist or not

Step 6: Create email_valid.php for email address availability checking. This file receive the ajax post request for email address and check if the emailalready exist or not

Newsletter subscription using PHP, Ajax, jQuery

Must Read : Newsletter subscription using PHP, Ajax, jQuery

Step 1: In our first step we create our database table to store user email address.
CREATE TABLE IF NOT EXISTS newsletter_signup (
email_id int(11) NOT NULL AUTO_INCREMENT,
email_address varchar(50) NOT NULL,
PRIMARY KEY (email_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Setp 2: Now we create a subscription form where user put their email address. File name is index.php

Setp 3: Now we have to style our subscription form. So we create a style.css file for that

Step 4: Now we have to send user email address to a page. Here I use a js file to take email value from index.php and send the value using ajax post request to that page. So I create a addmail.js to do that.

Step 5: Now we create a connection file to connect php with mysql conn.php

Step 6: Now we create a php file which take the email value from addmail.js and insert the email address to our database table and return the status. So we create add_mail.php for that.

Step 7: In this file we fetch all email address from database table and show the list to administrator(just think). So we create list_email.php for that.

Ajax contact form using captcha validation

Must Read : Ajax contact form using captcha validation

Step 1: In this step I create contact form for user from where they will send contact to us

Step 2: In this setp I create a style.css for styling our form

Step 3: Now I create a send_mail.js for taking value after clicking “Send Mail” button. This js file take the value from the form , validate all fields and send all data to another php file using ajax post request

Step 4: Last step for our application. Here I create a email_send.php which takes all ajax post data, validate all data using php, if any error occurs, send it to the ajax file with error message. If all are ok then I send a success message to ajax file. I display the message using result id.