Jump to content

[SOLVED] Default Variable setting?


Dragoa

Recommended Posts

I don't know what to call this, so I can't really do any searching for it, but basically what I want to do is if I pass a function a variable, to have it take that variable, and if I don't, take a default one.

 

What is basically happening is I've got a header class which makes some change for my navigation for the site, and I don't want it to do for a certain page, but I want it to do this for every other page. But to make this easier, code below shows my desired result.

 

printCommand("Test"); //Would print Test to the screen.
printCommand(); //Would print Default to the screen.
printCommand("Test2"); //Would print Test2 to the screen.

 

I don't really wanna do any sort of hardcoding in these pages[so I can't just make an exception in the class itself], since it's bad style and essentially makes any sort of modification extremely tedious. Any help is greatly appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/111160-solved-default-variable-setting/
Share on other sites

The most basic way would to have something like

function doSomething($param = 'default value') {
  // function body
}

 

so if you just call doSomething() $param would be 'default value', but if you called doSomething('no use this'); $param would be 'no use this' instead.

 

Is that what your looking for?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.