Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.