- PHP echo and print Statements
- PHP echo and print Statements
- The PHP echo Statement
- Example
- PHP is Fun!
- Example
- » . $txt1 . «
- The PHP print Statement
- Example
- PHP is Fun!
- How to Print Document in PHP and JavaScript
- So Let’s do the coding.
- Before we started:
- Creating Database
- Creating the database connection
- Creating The Interface
- Creating PHP
- Creating the Main Function
- Set printable table to Print View automatically
- Print HTML in PHP
- PHP Echo Function
- PHP Print Function
- This is HTML markup
- Closing
- About the author
- John Otieno
PHP echo and print Statements
With PHP, there are two basic ways to get output: echo and print .
In this tutorial we use echo or print in almost every example. So, this chapter contains a little more info about those two output statements.
PHP echo and print Statements
echo and print are more or less the same. They are both used to output data to the screen.
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print .
The PHP echo Statement
The echo statement can be used with or without parentheses: echo or echo() .
Display Text
The following example shows how to output text with the echo command (notice that the text can contain HTML markup):
Example
echo «
PHP is Fun!
«;
echo «Hello world!
«;
echo «I’m about to learn PHP!
«;
echo «This «, «string «, «was «, «made «, «with multiple parameters.»;
?>?php
Display Variables
The following example shows how to output text and variables with the echo statement:
Example
echo «
» . $txt1 . «
«;
echo «Study PHP at » . $txt2 . «
«;
echo $x + $y;
?>
The PHP print Statement
The print statement can be used with or without parentheses: print or print() .
Display Text
The following example shows how to output text with the print command (notice that the text can contain HTML markup):
Example
print «
PHP is Fun!
«;
print «Hello world!
«;
print «I’m about to learn PHP!»;
?>?php
Display Variables
The following example shows how to output text and variables with the print statement:
How to Print Document in PHP and JavaScript
In this tutorial we will tackle on How To Print Document using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily learn its syntax. It is mostly used by a newly coders for its user-friendly environment.
So Let’s do the coding.
Before we started:
- First you have to download & install XAMPP or any local server that run PHP scripts. Here’s the link for XAMPP server https://www.apachefriends.org/index.html.
- And this is the link for the jquery that i used in this tutorial https://jquery.com/.
- Lastly, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
Creating Database
Open your database web server then create a database naming «db_print». After that, click Import then locate the database file inside the folder of the application then click ok.
Or, you can copy and paste the SQL script below in SQL page of the database.
Creating the database connection
Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it «conn.php».
Creating The Interface
This is where we will create the appearance of an application. To create this simply copy and write this block of code inside the text editor, then save it as index.php.
Creating PHP
This code contains the save query of the application. This code will store the inputs to the database server. To do that copy and write these block of codes inside your text editor and save it as «save_query.php».
Creating the Main Function
This code contains the main function of the application. This code will display the data that will be printed when the button is clicked. To do this all you have to do is copy and write the code inside the text editor and save it as print.php.
Set printable table to Print View automatically
If you wanted to set the «print.php» into Print View automatically. Copy and paste the code below after the PrintPage() function inside the «script» tag. The code will open in a new window and automatically viewed in print view after the data loaded.
The javascript code above helps also to automatically close the print page after printing or cancel printing.
There you have it we already learn on How To Print Document using PHP. I hope that this simple tutorial helps you understand the difficulties of PHP. For more updates and tutorials just kindly visit this site.
Enjoy Coding.
Print HTML in PHP
A fundamental use of the PHP programming language is to build dynamic web applications. Hence, we often need to print information in HTML inside a PHP web application.
This guide will learn about two main ways to print HTML content on a PHP web application.
PHP Echo Function
If you know the basics of PHP and get output, you know about the echo method. It is used extensively in PHP to output values on the screen. Although the echo method does not behave like a function, more like a constructor, you can add parenthesis to the statement if required.
Like any other valid PHP statement, you terminate it with a semi-colon. The echo method will take the content inside the quotes and print it to the screen.
You can use the echo statement to print strings, variables, results from expressions and functions, and more.
The examples below show how to use the echo statement.
The above examples contain multiple statements that can print strings, numbers, and variable interpolation.
If you want to print multiple statements, you can specify each of them separated by comma as shown in the example below:
To print HTML markup using the echo statement, we can pass the content to it as shown in the example below:
$version = «HTML5» ;
echo «This is valid » ;
?>?php>
The code above will print valid HTML content.
PHP Print Function
The other method we can use to print HTML markup in PHP is the print function.
It is similar to echo, except it behaves like a standard PHP function. It accepts a single argument meaning it can print a single string.
The following is a simple example of the print method.
Like echo, you can output strings, numbers, and variables using the print statement.
To print HTML content using the print statement, you can do:
print «
This is HTML markup
» ;
?>?php>
The above should process the markup and display it accordingly.
If you want to learn how to print PHP values and variables inside HTML, check our tutorial on the topic.
Closing
This tutorial taught you how to print HTML markup inside a PHP file. This helps make the web page dynamic as you can fetch information from the server and display it on the browser.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list