Forum Topic: Php: Constants

(968 views • 9 replies)

This topic is 1 page long.

<< < > >>
None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/18/06 06:29 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Php: Main - All your PHP needs!

In this tutorial, you will be learning how to create a constant, what a constant is, and how to use them.
-------------
What is a constant?
A constant is like a variable, except fore a few things, First, it is defined differently than a variable (you will learn how to define one later). Second, constants can not be changed or undefined. Third, constants can only hold string, integer/float (any number), and boolean values. And last, constants do not use the $ sign. Oh, and constants also don't follow variable scoping rules.

How do I create a constant?
To create a constant, you use the define() function. Example:

define(constant_name,constant_value);

Remember that constants follow variable rules when defining them. So, that means a constant name has to begin with a letter or underscore, not a number. Also, constants are cAsE-sEnSiTiVe so remember that ;). An example of defining a constant is below:

define("HELLO","world");

How do I output a constant's value?
To output a constant's value, you simply call its name. Say I have a constant HELLO with a value of world. If I wanted to output it, I would do:

echo HELLO;
//outputs world

If you tried something like this:

echo Hello;

It would output Hello, and you would get an error.

There is also another way to output a constant's value. This is done by the constant() function. Example:

echo constant(constant_name);

Example using the HELLO constant:

echo constant("HELLO");
//Outputs world

You are probably wondering why you would use constant() instead of just simply using the constant's name. This is because, for example, if you have the constant's name held somewhere dynamically in a variable. Look at this:

$constant="HELLO";
echo $constant;
//outputs HELLO

If you were doing something like this, you would probably want to output HELLO's value, not the exact word HELLO. So to do this, you would do:

$constant="HELLO";
echo constant($cons);
//outputs world

That is the best I can explain for using the constant() function.

Other functions to use with constants
There are a few other functions you can use with constants. I will list them and what they do below.

defined(constant_name);

The defined function is used to tell whether or not a constant exists. It will return true or false. Example:

define("WHAT","hi");
echo defined("RLY");
//outputs false because the constant RLY doesnt exist.

get_defined_constants()

This function returns an associative array or every defined constant. There is an optional parameter that can go in it, but I don't know anything about it, and don't feel like looking it up. Anyways, here is an example:

define("LOLWUT","dayum");
define("ORLY","yarly");
$constants=get_defined_constants();
echo $constants;
# theoretically, outputs:

dayum
yarly

but would really output Array #
-------------
That is the end of this tutorial! I hope you learned something new :)

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

cherries

Reply To Post Reply & Quote

Posted at: 7/18/06 07:51 PM

cherries LIGHT LEVEL 18

Sign-Up: 06/07/05

Posts: 4,576

also,

define("NAME", "VALUE", true);
define("NAME", "VALUE", false);

the third value means that it is case sensitive.


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/18/06 08:00 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/18/06 07:51 PM, -cherries- wrote: also,

define("NAME", "VALUE", true);
define("NAME", "VALUE", false);

the third value means that it is case sensitive.

ORLY? Thanks for that info :D

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

cherries

Reply To Post Reply & Quote

Posted at: 7/18/06 08:24 PM

cherries LIGHT LEVEL 18

Sign-Up: 06/07/05

Posts: 4,576

You also forgot to explain constants in class's:

class orly {
....const NOWAI = false;
}

echo orly::NOWAI;


None

cherries

Reply To Post Reply & Quote

Posted at: 7/18/06 08:40 PM

cherries LIGHT LEVEL 18

Sign-Up: 06/07/05

Posts: 4,576

define("LOLWUT","dayum");
define("ORLY","yarly");
$constants=get_defined_constants();
echo $constants;
# theoretically, outputs:

dayum
yarly

but would really output Array #

then do :

define("LOLWUT","dayum");
define("ORLY","yarly");
$constants=get_defined_constants();
print_r($constants);


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/18/06 08:59 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Cherries, stop fucking pwning me >:[

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

cherries

Reply To Post Reply & Quote

Posted at: 7/18/06 09:02 PM

cherries LIGHT LEVEL 18

Sign-Up: 06/07/05

Posts: 4,576

At 7/18/06 08:59 PM, SpamBurger wrote: Cherries, stop fucking pwning me >:[

kay :C

PWN PWN PWN PWN PWN PWN PWN PWN

None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 7/18/06 09:38 PM

Pilot-Doofy NEUTRAL LEVEL 37

Sign-Up: 09/13/03

Posts: 12,275

You should also mention that defining constants is not only slower while writing the code, but also slower in the execution processes. In my experience, constants are about 3 times slower as variables while being stored in the memory.

Now, of course that is very minute on a per-use basis, but over a large script it could slow things down.

Another important note is that constants aren't always appropriate but they can be very useful. If it can be stored in a variable, store it in a variable. That's as simple as it gets.

One useful (and sometimes destructive) feature of constants is that they're not block elements. What I mean by this is that if you have a code like so:

define('MY_CONST', 'Hey, my name is Dan.');
function outputName() {
echo MY_CONST;
}

Using a script to call the function written above, like so:
outputName();

would output "Hey, my name is Dan.".

However, if you try doing that with a variable instead of a constant, it will output nothing. Like I said, sometimes this can be useful sometimes it can be hazardous. I wouldn't reccommend using constants inside of functions anyway, but sometimes people have different "opinions" on things.

You could use this feature when you're working with classes as well. For instance, if you want to output the execution time of the page (which a lot of sites do), you could store the starting time in a constant then access that constant from inside a method within a class.

Let's look at this:

// This example would not work at all, but just showing you
define('START_TIME', microtime());

class BasicPage {
function OutputBody($bodyHTML) {
echo $bodyHTML . '<!-- other template objects -->
Page executed in ' . microtime() - START_TIME;
}
}

As I said, the example isn't a very good working example, but it shows you that you can still access a constant's value from inside a foreign code block. If you were to try storing the START_TIME value in a variable and accessing it inside of the OutputBody method within the BasicPage class, no value would be called.

Just my 2 cents on the matter.


None

authorblues

Reply To Post Reply & Quote

Posted at: 7/19/06 03:10 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,363

At 7/18/06 09:38 PM, Pilot-Doofy wrote: stuffs

@ doofy: (i ask you specifically, because i feel you would know best.) my use of constants is usually only for if i need access to a variable in a master document FROM an included file. now, i know that this works, but is there a better way? i like my way, but im wondering if im doing something wrong, where i cant get access to these values with NORMAL variables.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

NinoGrounds

Reply To Post Reply & Quote

Posted at: 7/19/06 04:29 PM

NinoGrounds EVIL LEVEL 17

Sign-Up: 11/28/05

Posts: 3,756

At 7/18/06 08:59 PM, SpamBurger wrote: Cherries, stop fucking pwning me >:[

Lol.

But really you should mention some more. Like Doofy. (not saying $this bad)


All times are Eastern Standard Time (GMT -5) | Current Time: 01:40 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!