I want to use a constant in PHP, but I also want to put it inside double quotes like a variable. Is this at all possible?
define("TESTER", "World!"); echo "Hello, TESTER";
obviously outputs "Hello, TESTER", but what I really want is something like:
$tester = "World!"; echo "Hello, $tester";
outputs "Hello, World!".
A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.
Q 2 - Which of the following is correct about constants vs variables in PHP? A - There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
A PHP constant is the opposite of a variable in that once it has been defined it cannot be changed. Constants are particularly useful for defining a value that you frequently need to refer to that does not ever change.
Single or double quotes in PHP programming are used to define a string. But, there are lots of differences between these two. Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written.
Sorry, that's not the way constants in PHP work. You can put variables in double quotes and heredocs but not constants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With