PHP forum

Is there a limit on the number of defines in PHP?

Apr 7, 2010 5:16 pm
Ikke

Hi everybody,

While going through the code of an OS website, I noticed all the links were
replaced by defines. Everytime someone wanted to use a link, said link was
added to an included file, and echo'd in the page where the link should be.

The entire include file (links.inc) consisted of lines like the following:
define('GOOGLE', '<a href="http://www.google.com/">Google</a>');

At first I liked the idea - if you want to change a link, you only have to
change it once, which is quite a benefit.

However, how many of these defines are allowed in PHP? I've had a quick
Google for an answer, but couldn't come up with a definitive answer.

Could somebody please tell me if there is a limit on the number of defines,
and if so, what that limit is?

Thanks in advance!

Ikke

Apr 7, 2010 5:38 pm
Jerry Stuckle
Re: Is there a limit on the number of defines in PHP?

Ikke wrote:
> Hi everybody,
>
> While going through the code of an OS website, I noticed all the links were
> replaced by defines. Everytime someone wanted to use a link, said link was
> added to an included file, and echo'd in the page where the link should be.
>
> The entire include file (links.inc) consisted of lines like the following:
> define('GOOGLE', '<a href="http://www.google.com/">Google</a>');
>
> At first I liked the idea - if you want to change a link, you only have to
> change it once, which is quite a benefit.
>
> However, how many of these defines are allowed in PHP? I've had a quick
> Google for an answer, but couldn't come up with a definitive answer.
>
> Could somebody please tell me if there is a limit on the number of defines,
> and if so, what that limit is?
>
> Thanks in advance!
>
> Ikke

More than you're likely to ever use. No hard limit, AFAIK, but you'll
run into other constraints such as memory usage.

The bigger problem will be parsing thousands of define statements which
will never be used in the current script.

==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Apr 7, 2010 6:49 pm
Lars Eighner
Re: Is there a limit on the number of defines in PHP?

In our last episode, <Xns9D53C42D442A7ikkehierbe@69.16.176.253>, the lovely
and talented Ikke broadcast on comp.lang.php:

> Hi everybody,

> While going through the code of an OS website, I noticed all the links were
> replaced by defines. Everytime someone wanted to use a link, said link was
> added to an included file, and echo'd in the page where the link should be.

> The entire include file (links.inc) consisted of lines like the following:
> define('GOOGLE', '<a href="http://www.google.com/">Google</a>');

> At first I liked the idea - if you want to change a link, you only have to
> change it once, which is quite a benefit.

> However, how many of these defines are allowed in PHP? I've had a quick
> Google for an answer, but couldn't come up with a definitive answer.

> Could somebody please tell me if there is a limit on the number of defines,
> and if so, what that limit is?

> Thanks in advance!

There are various limits to memory and file size which are set in
the php.ini file, the most pertinent of which are the memory size and the
maximum script execution time. These might be negotiable (or not) with your
web host. Of course if it is your own machine there are hardware
limitations behind the software limits, and you cannot do anything about
that except upgrade the hardware.

You certainly can handle megabytes of defines even on a fairly stingy
service, which is more links than you will have on even a large corporate
site. There is no definitive answer, since the limit depends on software
limitations set by the server and they underlying hardware limitations.

The problems are: the processing times involved in loading huge files of
defines and the issue of how you will maintain such files. You will bump
your head hard on these issues long before you will reach the theoretical
software and hardware limits. How large a text file can your text editor
handle? How large a text file can you reasonably edit?

If you want to compete with Google, you will have to --- as a practical
matter --- use a database where you can leverage the look-up and maintenance
problems with the database's indexing abilities. Which is to say, defines
are not the way to go with huge numbers of links.

Defines might be realistic if you have a few links that are used on almost
every page --- perhaps a few dozen at most. Even on a personal site, if you
have hundreds of links, many of which are used only once, you should be
using a database, if only for ease of maintenance.

Lars Eighner <http://larseighner.com/> Warbama's Afghaninam day: 126
When you explode with newline as a delimiter you get an extra, empty element
at the end if the string ends with a newline. Use rtrim on the string first.
-1 for explode limit will be wrong if the string doesn't end with a newline.
Apr 7, 2010 6:55 pm
Guest
Re: Is there a limit on the number of defines in PHP?

On Wed, 07 Apr 2010 13:38:45 -0400, Jerry Stuckle
<jstucklex@attglobal.net> wrote:

>Ikke wrote:
>> Hi everybody,
>>
>> While going through the code of an OS website, I noticed all the links were
>> replaced by defines. Everytime someone wanted to use a link, said link was
>> added to an included file, and echo'd in the page where the link should be.
>>
>> The entire include file (links.inc) consisted of lines like the following:
>> define('GOOGLE', '<a href="http://www.google.com/">Google</a>');
>>
>> At first I liked the idea - if you want to change a link, you only have to
>> change it once, which is quite a benefit.
>>
>> However, how many of these defines are allowed in PHP? I've had a quick
>> Google for an answer, but couldn't come up with a definitive answer.
>>
>> Could somebody please tell me if there is a limit on the number of defines,
>> and if so, what that limit is?
>>
>> Thanks in advance!
>>
>> Ikke
>
>More than you're likely to ever use. No hard limit, AFAIK, but you'll
>run into other constraints such as memory usage.
>
>The bigger problem will be parsing thousands of define statements which
>will never be used in the current script.

Is there any significant performance difference between using define()
and constants, versus assigning the values to variables? I use a
shared URL definition technique as well, but in this fashion :

$site_tld = 'www.domain.com';
$site_secure_url = 'https://' . $site_tld;
$url_contact_page = $site_secure_url . '/contact.php';

(The first two variables are used in other contexts, which is why
they're assigned separately.)

Would define()s be more or less memory efficient, or is the difference
so trivial that it's not worth real-world consideration?

Thanks.




Previous Thread: Trying to send mail with PEAR, php string problem
Next Thread: PHP Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in

Related Forum Topics
Defines not working
I have the following style of define in my code and they appear not to work:

define('myPrefix','Prefix') ;
define('item_1',myPrefix.'_1') ;
etc ...

The second define exapnds as myPrefix_1 instead of Prefix_1, any suggestions
what I am doing wrong?
Thanks,
Sid.



Re: Limit calls of script
In article
<cc71f800-41f1-4446-9190-620abde7de0d@r11g2000yqa.googlegroups.com>,
timg <gravesit@gmail.com> wrote:

> It gets called when the page loads via actionscript in a flash app.
> Here is the script that calls the php and parses the xml:
>
> var myXMLLoader:URLLoader = new...
Warning: Redirection limit for url exceeded...
Hi,

I created a loginsystem with php.The coresponding php script run on
local server very well. Now I maked a connect to an extern server. But
if I let run the php script on this extern server, I get the following
warning

Warning: Redirection limit for url exceeded. Unable to load the...
Formatting a number
Hello

I am looking if there is a better way to write the following if possible.

I would like to display the negative sign in front of the dollar sign
when the unit is negative.

if($units < 0){
$f_units = '-$' . number_format($units * -1);
} else {
$f_units = '$' ....
Variable number of args
I have a function that always has at least one argument and may
have a 2nd.

Can I pass the first by reference and then check to see if there
is a 2nd, e.g:

procedure foo($bar){

echo $bar;

$numargs = func_num_args();
if ($numargs) {
$bar2 = func_get_arg(1);
echo...
Re: Checking equal number of and
.oO(jwcarlton)

>Can you guys think of a good way for me to check a string to make sure
>there are an equal number of <div (.*)> and </div>? Then, either add
>or remove </div> tags as needed to make them match?

You could run the string through Tidy.

Micha


Phone number formatting
So, I have a list of phone numbers in various formats I.e. the users
have written them in different styles, with and without dashes, spaces
and so on.

Now I want to format them. I have a text file with all the area codes
for Sweden, so I remove any non-digit character from the phone...
Query shows only one telephone number
Hi All,

I have a php file with a query that should show me all member
cellphone numbers who
have stated they want to receive a message. However there are more
members in the table
the form only shows one:

$connection = mysql_connect($db_host, $db_name , $db_password) or...
Function to return the next sequential number
I need a function that will return to each use of it the last number
returned plus one, with no time-share problem when multiple users are
accessing it simultaneously, and any problem or interruption of the
process of executing it by an earlier user will not interfere with its
use by...
Change a string where have number after a word
"arts 4, tested 4, passenger 2"

"arts 4, tested 4, passenger 2, arrived 2"

"arrived 2, tested 4, passenger 2, arts 4"

how can change this type of string for to have only the word with 4 at the
end?


so
"arts 4, tested 4"
first steps taht remove the words with 2 at the end;
and...