PHP: Save String as Variable and Print it

One of the most basic elements of PHP is the ability to save strings as variables and print them. A string is a sequence of characters, commonly used for text elements.

The Code

				
					<?php
// Save String as Variable
$string = 'Hello, World!';
?>
				
			

At this moment, we have saved the string “Hello, World!” as the variable $string. We can now use this variable to display the string anywhere we need to!

Variables

Variables start with the symbol “$”,  and are usually followed by the equal sign “=” when setting up a value. The statements must always be closed by a semi-colon “;”.

Variables can be Integers, Doubles, Booleans, NULL, Strings, Arrays, Objects, and Resources.

Print the variable

For this example we will use the print function. It is very easy to use and apply for simple strings.

				
					<?php
// Print the variable
   $string = "Hello, World!";
   print($string);
?>
				
			

Complete Code

Create a file called hello.php with the following code and open it on your browser.

				
					<!DOCTYPE html>
<html>
<head>
<title>Hello, World! Page</title>
</head>
<body>
<?php
   $string = "Hello, World!";
   print($string);
?>
</body>
</html>
				
			
Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on email

Related Posts