A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to get current directory, filename and code line number in PHP
Q:

How to get current directory, filename and code line number in PHP

0

How to get current directory, filename and code line number in PHP

PHP provides a large number of magical constants. All these constants are case sensitive. With the help of these constants, we can get the current directory, filename, current line number and much more. It is more secure, reliable, and fast, which has resulted in web developers preferring to work with PHP. So, it does not matter if the developer needs to know the line number or current directory of the PHP document.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

To get the current directory

__DIR__: This constant is used to get the current directory of the file. This is equivalent to the dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. If used inside an include method, the directory of the included file is returned.

<?php echo "The current directory of this file is '" . __DIR__ . "'.\n"; ?>

Output: The current directory of this file is 'C:\wamp\www\demo'.

To get the current filename with full file path

__FILE__: This is used to return the full file path and filename of the file. If used inside an include method, the name of the included file is returned.

<?php echo "The current file with path is '" .  __FILE__ . "'.\n"; ?>

Output: The current file with path is 'C:\wamp\www\demo\magic_const.php'.
This magic constant is mostly used in case where we have to include some files from the directory.

To get the current line number

__LINE__: This is used to return the current line number of the file.

<?php echo "The line number of this code is '" .  __LINE__ . "'.\n"; ?>

Output: The line number of this code is '34'.
This constant is mostly used while debugging the code or to get line number of the error.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now