How to use define variable inside a function?

Hello,

How to use define variable inside function

define(ZIPDIR, 'sammple');
function ornek($aaa, $bbbb, $cccc){
// How should ZIPDIR be called here?
GLOBAL ZIPDIR // Not working
}
ornek($aaa, $bbbb, $cccc);

You already “define” the variable so you do need to do it again.

Example

    public function __construct()
    {

        // Set your database credentials and options here
        $this->dsn = 'mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8mb4';
        $this->username =  DATABASE_USERNAME;
        $this->password = DATABASE_PASSWORD;
        $this->options = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
        ];
    }

DATABASE_PASSWORD is defined in my configuration file.

It’s best to avoid global variables as much as possible and it better to use const over define as define can be redefine latter on in the program code.

const ZIPDIR = "sample";

BTW how do you know that the function isn’t working? It’s doing nothing and returning nothing. I am assuming that you type that instead of copying / pasting the offending function, but you know what they say about assuming. :rofl:

I have this in the function and I get an error

$dosyayolu = str_replace(array(BACKUPDIR, ZIPDIR, DIZINDIR), $dizin, $dosya_yolu);

Error

PHP Fatal error:  Uncaught ArgumentCountError: str_replace() expects at least 3 arguments, 2 given in

Don’t use global variables. That is part of your problem.

1 Like

These are called defined constants. They have global scope, i.e. you can simply use them anywhere in your code after the point where they have been defined.

Well written functions should use call-time parameters for all input data, so that you can see, at the point of the call, what inputs it operates on, without needed to know about the inner workings of the function definition. This is known as the black-box function model.

The error has nothing to do with the use of defined constants (you would have been getting undefined constant errors.) The error has to do with the number of parameters being supplied to the str_replace() call. Either this code was copy/pasted from somewhere on the web and the comma separator(s) aren’t straight-ascii comma(s) and should be deleted and retyped, or the error is coming from some place else in the code (did you confirm the filename and line number in the error matches the code you are looking at) or from changes to the code that haven’t been saved, uploaded, or taken effect where the php code is being executed.

The syntax for the posted define() statement is incorrect and produces an error in itself. The rest of the posted code and the snippet of code where the error is supposedly occurring, don’t match. If you want anyone here to help, you need to post the actual code that reproduces the problem and the full error message.

1 Like

Thank you for the answer,
Since I was getting an error, I wondered if it was caused by the definition.
I didn’t copy and paste the code from anywhere, I wrote it myself and solved the problem.
I used str_replace this way elsewhere. str_replace(array($aaa,$bbb,$ccc), '', $string)

The problem was solved by changing the $dizin variable to array($dizin).

$dosyayolu = str_replace(array(BACKUPDIR, ZIPDIR, DIZINDIR), array($dizin), $dosya_yolu);
Sponsor our Newsletter | Privacy Policy | Terms of Service