5

I am creatting a Code Igniter project in which I want to pass a variable through the URL like a get statement, like this:
url: /site/cake/1
controller function: cake($var)
but when the variable is left blank, I receive an error, how can I get code igniter, to ignore this?

2
  • Could you post what errors you get? Commented Jan 31, 2010 at 21:56
  • A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Site::cake() Filename: controllers/site.php Line Number: 16 Commented Jan 31, 2010 at 21:59

2 Answers 2

17

In your controller, do this:

function cake($var = null) {
    // your other code here
}

When $var isn't present in the URL, it will be set to null and you'll receive no error.

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

3 Comments

Great - I'm glad :) If my answer helped, would you mind accepting it as the correct answer?
You should be setting it to null instead of false, as false is an actual valid value.
@Chetan: You're absolutely right. I've edited my answer. Thanks for pointing that out.
1

To explain why Colin's answer works: The issue you had, was that there was no default value for that controller function. In php, creating a default value for a function parameter is done by assigning it a value in the function definition ($var = false). Now when the cake() function is called with no parameter, it will set $var to false by default.

Comments

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.