Skip to main content

How to print result in PHP

In PHP there are various methods to print data. PHP provides more functions and language constructs for printing various data types. These functions are varied on the data format printed on the browser using them. For example, the print() and echo() statements prints string data, whereas, the print_r() is used to print compound datatype like arrays
 or objects. In this tutorial, we are going to see some of the PHP functions that are used to create print statements. I have added the example code for each function.

PHP Functions used for Printing Data

print() – The print() is used to create PHP print statement to print given data to the browser. It accepts single data and prints it on the browser. It is a PHP language construct and not a function. So, we can use ‘print’ without parenthesis for creating a print statement. The code shows an example for using print with and without parenthesis where both will print same data.
print "Apple"; 
//(or)
print("Apple");
echo() –  The echo() is also not a PHP function. It is also a construct as like as print(). The difference is that the echo() will accept multiple data separated by commas. While sending multiple values to the echo() statement, we have to use parenthesis to enclose the values. If we use single data in an echo() statement, we can ignore parenthesis. In the code, I use echo() statement for printing multiple data and stated the output using PHP comment line.
echo "Apple"; 
//(or)
echo("Apple");

// Output AppleOrangeGrapes
echo "Apple","Orange","Grapes"; 
// not a valid statement
echo ("Apple","Orange","Grapes");
printf(string_format, values) – This method is used print the formatted output by using the values passed as the parameter of this function. So, this functions accepts the output string format and the values to be added to be added. For example,
printf('We are expected to score above %d%% for distinction', 85);
//Output: We are expected to score above 85% 
//for distinction
sprintf() – This is similar to the printf() function except that it can return the formatted string instead of printing it to the browser. Then we can store it into a variable.
print_r() – This function is used to print the compound data like PHP array or objects.
var_dump() – var_dump() also prints array data in structured manner. It gives additional data, like, the data type, the length, values and more. For example,
var_dump(false) // prints bool(false) 
print_r(false) // returns empty string

Comments

Popular posts from this blog

Build chatbot with node js and react js

User Experience is given a lot of attention while building any application these days. More and more brands are leveraging chatbots to service their customers, market their brand, and even sell their products. There are a lot of awesome tools out there which helps in building an intelligent bot very easily like Google’s DialogFlow, Amazon Lex, etc, most of which implement their own Natural Language Processing (NLP) logic. However, in some cases, we don’t really need an intelligent bot. Whenever we have a small application having a limited set of options to choose from, it’s not really necessary to use NLP based tools like Google’s DialogFlow. You need to integrate with them (which is pretty easy though), and you need to make a network call to get the results. Instead, you would want to define your rules locally in those cases. Here we will build a simple chatbot using React Simple Chatbot library and add it to our pizza-builder app using which we can build ou...

PHP Image Upload with Size Type Dimension Validation

File upload feature requires basic validations to  make clean and hygienic  the user input. There is a huge chance of exploiting a file upload option with malicious intent. Improper implementation of a file upload input increases security vulnerability. We need to validate the uploaded files before saving them on the server to reduce the vulnerability. I have created a HTML form and provided an option to upload files. When the form is submitted, the file binaries are sent to the PHP and validated in the server side. I have checked if the uploaded file is an image and I have specified the allowed image extension, size and dimension based on which the validation is taking place. After all these validations have passed, the image file is saved in the target location as specified. The server-side image file validation takes place in the following aspects. If the file is not empty. If the file extension is one of .jpg, .png, .jpeg. If the file size is le...

Arrays In PHP

Have you ever heard about Array.A n Array is a   PHP datatype  used to store a group of data into a variable. Each array item is stored as a key and value pair. Arrays are classified as “indexed array” and “associative array” based on the key specification. The Indexed array has default index starts with ‘0’ and the associative array contains the user-defined key index. The array can also be classified as “one-dimensional array” and “multi-dimensional array” based on the level of the array index. In this tutorial, we are going to see some of the important array functions in PHP. PHP Array Functions In this section, we are going to see some of the widely used PHP array functions. I explain the purpose of the array functions, the parameter it requires and the default values of the parameter. This section includes small and simple PHP code snippets to explain the usage of each array function.  range(start, end) PHP  range()  fu...