Is there any difference between __DIR__ and dirname(__FILE__) in PHP? - php

It looks the same for me,but I'm not sure,
because there are many projects that uses dirname(__FILE__).

Their result is exactly the same ; so, no difference on that.
For example, the two following lines :
var_dump(dirname(__FILE__));
var_dump(__DIR__);
Will both give the same output :
string '/home/squale/developpement/tests/temp' (length=37)
But, there are at least two differences :
__DIR__ only exists with PHP >= 5.3
which is why dirname(__FILE__) is more widely used
__DIR__ is evaluated at compile-time, while dirname(__FILE__) means a function-call and is evaluated at execution-time
so, __DIR__ is (or, should be) faster.
As, as a reference, see the Magic constants section of the manual (quoting) :
__DIR__ : The directory of the file.
If used inside an include, the
directory of the included file is
returned. This is equivalent to
dirname(__FILE__). This
directory name does not have a
trailing slash unless it is the root
directory. (Added in PHP 5.3.0.)

Related

I'm getting back into PHP however the below syntax is escaping me

So I've been forced back into a PHP program...it is SLIM 4 based.
<?php
(require __DIR__ . '/../config/bootstrap.php')->run();
I'm unsure of the (require) syntax above...look slike short for a function....but..not sure.
require() is an include() that fails if the file cannot be included.
If the included PHP file ends with a return statement, it will return this value to the caller.
In your case, it looks like bootstrap.php returns an object with a run() method.
Note that require() is not actually a function but a language construct, so the parentheses are optional; these 2 lines are therefore equivalent:
require('script.php');
require 'script.php';

How to separate filename from path? basename() versus preg_split() with array_pop()

Why use basename() in PHP scripts if what this function is actually doing may be written in 2 lines:
$subFolders = preg_split("!\\\|\\/!ui", $path); // explode on `\` or `/`
$name = array_pop($subFolder); // extract last element's value (filename)
Am I missing something?
However I guess this code above may not work correctly if file name has some \ or / in it. But that's not possible, right?
PHP may run on many different systems with many different filesystems and naming conventions. Using a function like basename() guarantees (or at least, is supposed to guarantee) a correct result regardless of the platform. This increases the code's portability.
Also, compare a simple basename() call to your code - which is more readable, and more obvious to other programmers in regards to what it is supposed to do?

Require_Once gives PHP Division By Zero Error

I'm trying to require a config file in index.php by using:
require_once __DIR__/application/common/config/Config.php;
However, PHP error logs state division by zero. The full path is /var/www/application/common/config/Config.php
How do I ensure this path is correctl represented in my require_once statement?
You need to use quotes for strings in PHP...
require_once __DIR__ . '/application/common/config/Config.php';
require_once __DIR__ . '/application/common/config/Config.php';
that should work? The "." concatenates the string. Basically " dir " (plus) 'other values'. and the other values need to be in quotes, otherwise it will take "/" as a basic math operator, thus trying to divide everything by 0 in the rest of the statement.
(require_once DIR/0/0/0/0;) and you can't divide by 0 ;)
This happens because of a series of design mistakes in PHP.
PHP converts every symbol it does not know to a string, except it has a leading upper character, that will be a constant.
Constants that are not defined are NULL
When you try any mathematical operation on a string, it gets converted to an integer.
"123" will be 123
"123 abc" will be 123 as well
"abc" will be 0.
When you try any mathematical operation on NULL, it gets converted to 0
So what happens, is that PHP calculates "yourdirectory"/0/0/0/0, which is a division by zero. (Ouch! Did I already tell you that PHP is a broken language?)
Just put the string in quotes.
require_once __DIR__."/application/common/config/Config.php";

Is it possible to disjunct two require_once statements with OR operator to use the latter as fallback?

Is there a way to perform double require_once statement where the second one as a fallback if the first one fails?
For example: I can do this
mysql_query() or die("Boo");
Could I do:
require_once('filename') or require_once('../filename');
If one fails it reverts to the other?
You can't do this because of a weird little quirk in PHP. require_once() is a language construct, not a function. It will interpret the whole line as the argument:
(('filename') or require_once('../filename'))
(added braces for clarity) the result of this operation is 1.
Use is_readable() instead.
if (is_readable($filename))
require_once($filename);
else require_once("../$filename");
or a shorter version that should work:
require_once(is_readable($filename) ? $filename : "../$filename");
#Pekka is correct. I'd add something else though, that might get rid of the issue entirely. If you have files in several different places, you can edit the include paths so that require() and include() look in a variety of places. See set_include_path()
The example in the manual adds a new path to the existing include path:
$path = '/usr/lib/pear';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

How does PHP uses constants (defines)

Imagine I have this constant in PHP:
define('APP_PATH', str_replace('//', '/', str_replace('\\', '/', dirname(__FILE__) . '/')));
When I use APP_PATH in my application, does PHP execute the code (dirname, and the two str_replace on __FILE__) each time or PHP execute the code once and store the result in APP_PATH ? I hope I'm clear enough :)
This question applies to PHP 5.1.0+.
It should be done once, at the time it was defined.
UPDATED
For documentation: define() - constants
From the documentation:
A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.
If you want more information on constants go ahead and read the documentation, it is explained pretty well there and probably has usage examples.
if you want a variable rather than a function you could make this an anonymous function
$APP_PATH=function(){ return str_replace('//', '/', str_replace('\\', '/', dirname(__FILE__) . '/') }
or
$APP_PATH=function($file){ return str_replace('//', '/', str_replace('\\', '/', dirname($file) . '/') }
which you could call with $APP_PATH [without variables] or $APP_PATH(FILE)
depends on what you wanna do
It executes it once and stores the result in APP_PATH. From that point on, APP_PATH is a scalar value. It's not like a handle to a computation/function or anything.
Tt is stored as the outcome in a single request, at the moment of the define. Hence 'constant'. The next request / script invocation will run the code again, so between requests it could be inconsistent.

Categories