PHP Tutorial
- Introduction to PHP
- Install PHP
- PHP Code
- PHP Echo
- PHP Print
- PHP Echo vs Print
- PHP Variable
- PHP Variable Scope
- PHP $ and $$
- PHP Constants
- PHP Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- Control Statement
Control Statement
- PHP If else
- PHP Switch
- PHP For Loop
- PHP foreach loop
- PHP While Loop
- PHP Do While Loop
- PHP Break
- PHP Continue
PHP Programs
- Pattern Programs
- Numbers Programs
- String Programs
- Array Programs
PHP Functions
- PHP Functions
- Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Arguments
- PHP Variable Arguments
- PHP Recursive Function
PHP Arrays
- PHP Array
- PHP Indexed Array
- PHP Associative Array
- Multidimensional Array
- PHP Array Functions
PHP Strings
- PHP String
- PHP String Functions
PHP Math
- PHP Math Functions
PHP Form
- PHP Form: Get Post
PHP Include
- PHP include & require
State Management
- PHP Cookie
- PHP Session
PHP File
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append File
- PHP Delete File
PHP Echo
PHP’s `echo` is a language construct, not a function, so parentheses are unnecessary when using it. However, if you wish to echo more than one parameter, you must enclose them within parentheses.
Here’s the basic syntax for using echo in PHP:
echo expression;
Where expression can be a string, a variable, or a combination of both. You can also echo multiple expressions separated by commas:
echo expression1, expression2, ...;
Here,
- echo: This is the language construct used to output content.
- expression: This represents the content you want to output. It can be a string enclosed in quotes (‘single’ or “double”), a variable containing a string or other data types, or a combination of strings and variables.
- The echo statement is utilized to output content.
- It can display strings, multi-line strings, escaped characters, variables, arrays, etc.
- echo is a statement, not a function, used for displaying output.
- It can be written with or without parentheses, i.e., echo or echo().
- Unlike functions, echo doesn’t return any value.
- Multiple strings can be passed to echo, separated by commas.
- In terms of performance, echo is generally faster than the print statement.
Printing a Simple String:
Output:
Hello, world!
Printing a Multi-Line String:
There are two ways to print a multi-line string in PHP:
- Using Double Quotes with Newlines:
2. Using Heredoc Syntax (Triple Quotes):
Both methods produce the same output:
This is a multi-line string.
It can span across multiple lines.
Printing Escaping Characters:
To print special characters like quotation marks within a string, you need to escape them using a backslash (\) before the character.
outputs:
This string contains an escaped quotation mark "
Printing Variable Value:
Output:
Hello, Maruti
In this example, the variable $name is interpolated directly into the string using double quotes.