answersLogoWhite

0


Best Answer

Outside of a class definition, constants can be declared with the define function. The function has two arguments; the name of the constant (a string), and the value of the constant (any variable type).

Inside a class definition, constants can be declared with the const keyword. Type the word const, the name of the constant in the form of how it will be called later, an "equals sign," then the desired value of the constant.

In both cases, here is an example.

// Outside of a class definition

define('foo', 'Value!');

echo foo; // Prints "Value!"

// Inside a class definition

class example {

const bar = "Value!";

}

echo example::bar; // Prints "Value!"

?>

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you declare a constant using PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Math & Arithmetic

How do you define a constant in PHP?

You can define a constant using the define() directive.you can use this a number of ways;to define a variable to a constant do:$string = "hello";define("string",$string);to define a string to a constant use:define("hello","there");to define a integer or other numerical value use:define("number",1.0);Summery:to define a string use quotes as you would do a string.Unlike variables in PHP a constant cannot be changed or undefined once it is defined. Constant remains automatically globally throughout the script. It means that it can be accessed from inside a function. e.g.


What is 10 percent in Php 250?

Answer: Php 2510% of Php 250= 10% * Php 250= 0.10 * Php 250= Php 25


What are the syntax and semantics of the include construct?

The PHP syntax and semantics are the format (syntax) and the related meanings (semantics) of the text and symbols in the PHP programming language. They form a set of rules that define how a PHP program can be written and interpreted. PHP is a procedural and object-oriented language (OOL) for coding webpage markup text to be transformed into HTML format on computerized devices. In later releases, PHP generates some code to be run by the Zend Engine, beyond using just HTML markup text. The syntax of PHP changed to include OOL keywords in versions PHP 3 and PHP 5.


How do you find version of PHP in uniform server?

The easiest way is to create a file with a name ending in .php (such as info.php), containing only these 3 lines: <? phpinfo(); ?> Upload this file to your web space and access it using your browser. It will show a lot of information about your webserver and the php version running on it.


What is the use of a question mark in php?

It is used to tell the sever to use the PHP parser. To begin php you must use <?php, and to end it, it is ?>.

Related questions

How can a PHP static be initialized?

PHP static can only be initialized using a literal or constant. You can not use an expression. You can initialize it to an integer but you may not to another variable.


Are you required to initiate a variable when you first declare it in php?

no


Does android supports PHP?

It supports websites made on php but not coding in php. There are workarounds for coding in PHP using Android.


Is Answers.com coded using PHP or ASP?

If you're referring to Answers.com, it's built using PHP as a backend.


How can you declare the variable in PHP?

Variables in PHP do not need to be declared like some languages (e.g. JavaScript). This is all that needs to be done to assign a variable: $variable = "Value";


How do you add a comment in PHP?

< ?php // This is an example of comment in PHP /* This is another example of comment in PHP and we can write comments in multiple lines using this method */ ? >


Why do we use Php?

PHP is an Open Source Scripting Language. We can create Web Pages and Web Applications using PHP.


How to display a variable in PHP using echo or print statements?

displaying a variable in php using echo statement? <?php $name="ram"; //declaring and defining the variable echo "$name"; //printing the variable using echo command ?>


How to make constant name case insensitive in PHP?

Generally, constant names are case sensitive in PHP.But... you can do a trick. If you will be consistent and all constant name will be defines as uppercase, you can access them using a combination of constant() and strtoupper() functions. Look at this example:?phpdefine(MY_CONSTANT, "HELLO");echo constant(strtoupper(my_constant));echo "";echo constant(strtoupper(My_Constant));echo "";echo constant(strtoupper(my_CONSTANT));?>


How do you output a star using PHP?

type this in a file save it as something.php and then run it <?php echo '*'; ?>


How do you create static variables in PHP?

Use the "static" keyword to declare a static variable within a function like shown below. <?php function fun() { static $variable; static $another_variable = ''; } ?>


Can you use an ASP textbox using PHP?

No