Variables in PHP || Server Side Scripting || BCIS Notes

Variables in PHP || Server Side Scripting || BCIS Notes

Variables in PHP

Variables in PHP are used to store data, like strings of text, numbers, etc. Variable values can change over the course of a script. Here’re some important things to know about variables:

  • In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
  • After declaring a variable it can be reused throughout the code.
  • The assignment operator (=) used to assign value to a variable.In PHP variable can be declared as: $var_name = value;

Variable

Constant in PHP

A constant is a name or an identifier for a fixed value. Constants are like variables, except that once they are defined, they cannot be undefined or changed

Constant

PHP Echo and Print Statements

The echo statement can output one or more strings. In general terms, the echo statement can display anything that can be displayed to the browser, such as string, numbers, variables values, the results of expressions, etc.

Since echo is a language construct not actually a function (like if statement), you can use it without parentheses e.g. echo or echo(). However, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.

The echo statement

Print Statement

Display Variables

Display Variable

The PHP print Statement

You can also use the print statement (an alternative to echo) to display output to the browser. Like echo, the print is also a language construct not a real function. So you can also use it without parentheses like print or print().

Both echo and print statement works exactly the same way except that
the print statement can only output one string, and always returns 1. That’s why the echo statement considered marginally faster than the print statement since it doesn’t return any value.

If you liked our content Variables in PHP, then you will also likeĀ Introduction to PHP

Be the first to comment

Leave a Reply

Your email address will not be published.


*