2

How can I set a default variable to a function in PHP? In python, it would look like this:

def fun(con = predefined):

I've tried the following to no avail:

function fun($con = $GLOBALS['con']){}

In this instance, as you can see, I would like to set the value to the definition of a global variable.

3
  • check this Commented Feb 11, 2014 at 1:01
  • @DrixsonOseña Damn, apparently my keywords were off. I considered setting it at the beginning of the function, but coming from python; that looks too messy to me LOL. I'm going to leave this question for anyone else who does a specific search. Commented Feb 11, 2014 at 1:05
  • More specifically as well, I'd have preferred if the value of $con could be passed to the function as an option; looks like I'll have to do a workaround. Commented Feb 11, 2014 at 1:06

3 Answers 3

2

This is a very simple example:

 <?php
 function makecoffee($type = "cappuccino")
 {
     return "Making a cup of $type.\n";
 }
 echo makecoffee();
 echo makecoffee(null);
 echo makecoffee("espresso");
 ?>

The above example will output:

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the great explanation, it's publishable! But sadly it's not perfectly compatible with my issue.
this is a small example how you can achieve this functionality. Based on that you can developed your logic
1

Looks like your problem is that you're trying to set a default from a global. PHP may not like this. You can circumvent it this way:

function fun($con = null){
    $con or $con = $GLOBALS['con'];
}

I personally think this is kind of hokey. It looks like you're wanting to get a DB connection from a global resource. I don't think that's how this should be done, but this answer would allow $con to be set from global if not passed to the function. Or allow it to be used if passed.

3 Comments

This solution worked perfectly, though you have me concerned. Is there an alternative way to do it? My variable name gave it up, huh? I'd rather not have to redefine it every time, so I was looking for a way to define it indefinitely; global namespace looked like my only solution.
Are you familiar with classes? You could make a db class that maintains the connection and your class methods could access the connection using something like $this->con
Quing that's a great idea. I'm gradually moving across from python, so I have an understanding of how classes 'probably' work in PHP. Looks like an area for research, thanks for the tip!
1

We build a small class to use the globals in a static way

class globy{
    static public function set($name, $value) { $GLOBALS[$name] = $value; }
    static public function get($name)         { return $GLOBALS[$name]; }
}

then you can do this

function fun($con=globy::get('con')){}

or you can do this (not your question)

function fun($con=globy::set('con','value_of_con')){}

2 Comments

That's well beyond my understanding of PHP LOL, but it works as well as the other solutions; thanks very much.
Is it now more understandable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.