A variable can be seen as a container for some data. Variables in PHP start with the $ sign. Check the following script:
We basically created two variables, assigned a value to each of them, and then printed the sum on the screen.Code:<?php $x = 10; $y = 20; echo $x + $y; ?>
PHP is a dynamically typed language, which means that it’s variables don’t have any specific type. This means that the same variable can start holding an integer, and later on you can make it hold a string, for example.
Mixing Variables and Strings
As I mentioned before, double quoted strings expand variables inside them. Take a look at the code below:
The output is “Hello World”, because the variable gets expanded inside the string. Even variables holding integers will be expanded on the string.Code:<?php $var = "World"; echo "Hello $var\n"; ?>
String Concatenation
Another important point is string concatenation. Often times you’ll need to concatenate strings together, and the easiest way of doing it is with a dot.
Code:<?php echo "Hello" . "World\n"; ?>
or
The above code first adds x with y, then converts the result into a string, and then concatenate it with the “\n” string, effectively printing out 30 with a newline after it.Code:<?php $x = 10; $y = 20; echo $x + $y . "\n"; ?>


Reply With Quote

Bookmarks