Jump to content
Larry Ullman's Book Forums

Require Usage For Constant


Recommended Posts

Hello,

 

l have recently got Effortless E-Commerce this book,

however, when l read 'Selling virtual products' chapter, l come up with a question:

 

the code:

 

<?php

require('../inc.config.php') --> Constant 'MYSQL' defined in this file

 

require(MYSQL); --> require constant

 

...

...

...

?>

 

with this code, since the constant have been defined in inc.config.php, the MYSQL constant should have a global scope to be used and it could be access anywhere using 'MYSQL'.

 

Why bother require(MYSQL) again in this script ?

 

 

i am not quite understanding the usage of this ==

 

hope anyone could provide expert view.

 

Thanks...

Link to comment
Share on other sites

I may be misunderstanding your question, but it seems like you are misunderstanding the meaning of the require function.

The require function does not redefine the constant MYSQL; it actually includes the file that is linked to by the string defined by the MYSQL constant.

 

The require function is similar to the include function in PHP, and both are used to basically drop all the code in the included file into the PHP script calling the include/require function at the exact point the function is called. For more details, please see the following:

http://php.net/manua...ion.require.php

 

As such, "require(MYSQL);" takes all the code in the file linked to by the string stored in the MYSQL constant (i.e., the database-connection script), and places all that code in your script at that point. More specifically, this require function executes the code in the DB-connect script and establishes a connection with the DB, which you can then use throughout the rest of the script.

 

Basically, if you want to access the DB anywhere in the script, you need to execute "require(MYSQL);" before that.

 

Does that answer your question?

  • Upvote 3
Link to comment
Share on other sites

Thanks a lot for your answer..

Maybe l am not fully understand the language, pls correct if l am wrong

 

 

but my question come up is :

 

<?php

require('../inc.config.php') --> Constant 'MYSQL' defined in this file // e.g. define('MYSQL', 'connection string here');

 

 

// now can use MYSQL anywhere in script

echo MYSQL; //output MYSQL connection string or use MYSQL for connection

?>

my question is if l had already include '../inc.config.php', l already can use 'MYSQL' constant for my connection string anywhere in my script,

why should l write require(MYSQL) again ?

 

Thanks

Link to comment
Share on other sites

Because it's just a constant holding a string. You need something called a resource, not a text string. A resource is created when the string (that is an URI to a script) is included, and the actual MySQL connection resource resource is created. The creation of this resource is happing inside the script you find by this URI.

 

Maybe things would be easier to understand if we did this in config.php:

 

$mysql = "path_to_connection_script.php";
$images = "path_to_image_folders";
define ('A_CONSTANT', "holding a string");

 

And a simple mysql script:

$mysql_resource = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
if ( ! $mysql_resource) {
die('Could not connect: ' . mysql_error());
}

 

And this was your script:

// Require config file holding variables and a constant
require 'config.php';

// Echo out the contents of the variable and constant
echo $mysql; // output: path_to_connection_script.php
echo A_CONSTANT; // output: holding a string

// Now require the resource
require $mysqli; // require 'path_to_connection_script.php';

// Now we have the mysql resource in $mysql_resource
$result = mysql_query($query, $mysql_resource); // Here we can use the resource to run a query

 

What this means is that a constant and a string is pretty much the same thing. The constant is just a way for you to centralize where to put the require paths for your scripts. You could just use something along:

 

define('MYSQL', 'path_to_connection_script.php');

// These two lines below are equal
require 'path_to_connection_script.php';
require MYSQL;

 

As you can see now, you are just getting this constant to use in your script. The whole point about a constant is that it is interchangeable. You can't define the same constant twice. This means that, if you require the config file, you are sure the MYSQL constant you find there will hold the value (an URI) needed for getting the mysql resource.

 

Edit: This may seem complicated, but a resource is only another data type like Strings (text) and integers (numbers). The whole point is that for running a query, you need the resource as it holds vital info for PHP internally. You don't really need to know how it works on the inside. The resource is simply created when calling the mysql_connect() function, and then you are ready to roll with queries.

 

Hope that helps.

  • Upvote 3
Link to comment
Share on other sites

why should l write require(MYSQL) again ?

 

You're not writing "require(MYSQL);" again; you're writing it for the first time. That's the point.

If you don't write "require(MYSQL);", then you won't have a database connection.

Try deleting or commenting out the "require(MYSQL);" line and see what happens.

Link to comment
Share on other sites

Thanks...Antonio,,,

 

fabulous thats exactly my question is.... thx a lot for explain ..

l now have the idea of constant defining...

 

 

Thanks.. HartleySan,,

 

coz l had got confusion of using Constant for string and resource, thought string & resource are same type and usage..

l now got the idea of it, thx

 

 

you guys are truly PHP Experts....

Link to comment
Share on other sites

 Share

×
×
  • Create New...