Saturday, January 5, 2013

Types of Print Function in PHP


Introduction
PHP is a server side scripting language for creating dynamic web pages. There are so many functions available for displaying output in PHP. In this article I will explain some basic functions for displaying output in PHP. The basic functions for displaying output in PHP are as follows:
  • Print() Function
  • Echo() Function
  • Printf() Function
  • Sprintf() Function
  • Var_dump() Function
  • Print_r() Function
Print() Function 
Using this function we can display the outputs in the browser. This function returns the Boolean value true. We cannot print the multiple statements using this function. The print function plays the same role as the echo function.
Example1

<
body bgcolor="pink">print "Welcome Vineet Kumar Saini !!";?></body>
 Output
In the above example we display a simple message using the print() function.
1st.jpg
Example2
<html>
<
body bgcolor="pink">$rval="Welcome Vineet Kumar Saini is MCA Qualified !!";print $rval;?></body>
</
html>
Output
In the above example we display the message using a variable and the print() function.
2nd.jpg
Example3

<
body bgcolor="pink">print "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";?></body>
Output
In the above example we printed two strings using commas but the print() function doesn't display this message.
3rd.jpg
Echo() Function
The echo() function outputs one or more strings. Using this function we can display multiple statements in the browser. The echo() function is slightly faster than print(). Because it won't return a value.
Example1

<
body bgcolor="pink">echo "Welcome Vineet Kumar Saini !!";?></body>
Output
In the above example we display a simple message using the echo() function.
4th.jpg
Example2

<
body bgcolor="pink">echo "Welcome Vineet Kumar Saini is "," ","MCA Qualified !!";?></body>
Output
In the above example we print two strings using commas through the echo() function but the print() function doesn't display this message. It is the main difference between the echo() and print() functions.
5th.jpg
Printf() Function
The printf() function is also used in C, C++. The printf() function outputs a formatted string. Using this function we can display the output with the help of the formats specified.
Example
<html>
<
body bgcolor="pink">$name="Vineet Saini";
$age=24;
printf(
"The age of %s is %d years.",$name,$age);?></body></ html>
Output
In the above example we display the value of two variables i.e. string and integer using the printf() function.
6th.jpg
sprintf() Function
The sprintf() function writes a formatted string to a variable. This is the same as printf, but instead of displaying the output on the web page, it returns that output. sprintf() prints the result to a string.
Example
<html>
<
body bgcolor="pink">$name="Vineet Saini";
$age=24;
$rv=sprintf (
"The age of %s is %d years.",$name, $age);echo $rv;?></body></ html>
Output
7th.jpg
var_dump() Function
The var_dump() function displays information of a variable that includes its type and value. This function displays the variable value along with the variable data type. The var_dump() function is used to display structured information (type and value) about one or more variables.
Example

<
body bgcolor="pink">$name="Vineet Saini";
$age=24;
var_dump($name);
var_dump($age);
?></body>
Output
In the above example we declare two variables i.e. one is string and second is integer variable. The var_dump() function shows the types of these variables.
8th.jpg
print_r() Function
The Print_r() PHP function is used to return an array in a human readable form. This function displays the elements of an array and properties of an object. The print_r() function displays human-readable information about a variable.
Example

<
body bgcolor="pink">$arr=array ("Vineet","Kumar","Saini");
$arr1=
array(10,20,30);
print_r($arr);
print_r($arr1);
?></body>
Output
In the above example we display the element of an array using print_r() function.
9th.jpg
Conclusion
So in this article you saw various types of print functions. Using this article one can easily understand print functions in PHP.

Insert Value From CheckBox in Database (MySQL) in PHP


In the last article you saw how to insert a value from a radio button in the MySQL database in PHP. Now in this article you will see how to insert a value from a checkbox in the MySQL database. Using a checkbox you can insert multiple values in the MySQL database in PHP.
First of all we will create a database and a table in MySQL.

Create Database

1st.jpg

Create Table

2nd.jpg
Create config.php file

Now we will create a config.php file for connecting the database to all PHP files.

img3.jpg
Create the form.php file

Now we will create a form.php file by which we will insert a value through checkboxes in the MySQL database.

img2.jpg

Create checkbox.php file

No we will create a PHP file in which we include a config.php file and write the insert command for inserting the data in the database.

img1.jpg

Output

For running the above code we will write in the web browser "http://localhost/foldername/form.php" .

form.jpg

Now we select any checkbox. Suppose we select four checkboxes i.e. Vineet Saini, Ravi Sharma, Rahul Dube, Priyanka Sachan. Then we will click on the submit button.

form2.jpg

When we click on the submit button then you will get a message i.e. Record is inserted.

form3.jpg

Now you will see there are four records inserted in the database. Like as in the following image.

last.jpg

Conclusion

So in this article you saw how to insert multiple records in a MySQL database through a checkbox. Using this article one can easily understand insertion of multiple data in a MySQL database.