/    Sign up×
Community /Pin to ProfileBookmark

When Is It Necessary To Use Default Variables..

Hello again…

This is my second post in the last week and there will be many more because I just finished this PHP 5 Book By O’Reilly and I have tons of questions about what I’ve learned…

So as the title states, I need the skinny on when default variables are really needed w/ some examples possibly.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMay 10.2010 — By default variables, I assume you mean default values for function arguments? I also assume you are talking about functions you are writing yourself?

There is no requirement for you to provide default values. You would choose to do so on a case-by-case basis if it makes sense to have a default provided. To give you an example, if you are writing a function to connect to a database, one of the arguments might be the database driver to use. This is a case where you might want to provide a default, say MySQL, since the majority of people will be using MySQL. Similarly you would probably set 'localhost' as the default server location. Username and password however, you might not provide defaults for since these values might be unique to the individual.

[code=php]function db_connect($username, $password, $host = 'localhost', $driver = 'mysql') {
// -->
}[/code]


You will notice that I've used words such as "might" a lot, and this should indicate that there are no hard rules here. Just provide a sensible default value where it makes sense to do so.
Copy linkTweet thisAlerts:
@ChuckBauthorMay 10.2010 — By default variables, I assume you mean default values for function arguments? I also assume you are talking about functions you are writing yourself?

There is no requirement for you to provide default values. You would choose to do so on a case-by-case basis if it makes sense to have a default provided. To give you an example, if you are writing a function to connect to a database, one of the arguments might be the database driver to use. This is a case where you might want to provide a default, say MySQL, since the majority of people will be using MySQL. Similarly you would probably set 'localhost' as the default server location. Username and password however, you might not provide defaults for since these values might be unique to the individual.

[code=php]function db_connect($username, $password, $host = 'localhost', $driver = 'mysql') {
// -->
}[/code]


You will notice that I've used words such as "might" a lot, and this should indicate that there are no hard rules here. Just provide a sensible default value where it makes sense to do so.[/QUOTE]


Alright..got it...thanks..I'll have one or two more posts soon...this is just to clear some things up...I learn by reading first, documenting concerns, q&a, then development (which will make things even clearer)...but thanks.
Copy linkTweet thisAlerts:
@knight07May 10.2010 — By default variables, I assume you mean default values for function arguments? I also assume you are talking about functions you are writing yourself?

There is no requirement for you to provide default values. You would choose to do so on a case-by-case basis if it makes sense to have a default provided. To give you an example, if you are writing a function to connect to a database, one of the arguments might be the database driver to use. This is a case where you might want to provide a default, say MySQL, since the majority of people will be using MySQL. Similarly you would probably set 'localhost' as the default server location. Username and password however, you might not provide defaults for since these values might be unique to the individual.

[code=php]function db_connect($username, $password, $host = 'localhost', $driver = 'mysql') {
// -->
}[/code]


You will notice that I've used words such as "might" a lot, and this should indicate that there are no hard rules here. Just provide a sensible default value where it makes sense to do so.[/QUOTE]


I am new to php as well, I thought if you do not supply a default value for a variable, you get a undefined variable error?

Knight
Copy linkTweet thisAlerts:
@Jarrod1937May 10.2010 — I am new to php as well, I thought if you do not supply a default value for a variable, you get a undefined variable error?

Knight[/QUOTE]

The phrasing of the original post may be confusing to some. He is referring to default variable [I]values[/I] for functions, aka the variables get assigned a default value if they're not assigned one at the time of the function call.

Thus, you won't necessarily get an undefined error if you don't specify a default variable value, you will then only if you also then do not give the variable space a value when you're calling the function.
×

Success!

Help @ChuckB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...