Jump to content
phpsane

A Function Parameter That Takes In A Constant, Can It Always Take In A Variable Unless It Needs A Handle ?

Recommended Posts

Folks,

 

Q1. A function parameter that takes in a constant, can it always take in a variable, unless it needs a handle ?

 

While going through the Explode function tutorial, I did not come across anything that states the 3rd parameter (explode limit) in the Explode function has to be a string or a constant. Like so:

<?php
 
//Tutorial: http://www.tizag.com/phpT/php-string-explode.php
 
echo "Script 1"; //Using a Constant as 3rd parameter in the Explode function. Using FOR function.
 
$phrase = "Please don't blow me to pieces.";
$words = explode(" ", $phrase);
print_r($words);
 
for($i = 0; $i < count($words); $i++){
echo "Piece $i = $words[$i] <br />";
}
 
$explode_limit = 4;
$words = explode(" ", $phrase, 4);
for($i = 0; $i < count($words); $i++){
echo "Limited Piece $i = $words[$i] <br />";
}
 
print_r ($words);
 
?>
Edited by phpsane

Share this post


Link to post
Share on other sites

The only difference that matters here between a variable and a constant is that a variable can be modified and a constant cannot. If something does not need to modify a value then it doesn't matter which one you use.

  • Like 1

Share this post


Link to post
Share on other sites

(Forum has cut my post in half!). Now, need to re-type the other half of the post again. Very annoying! ...

 

Therefore, can we have a variable instead. Like so:

 

 

<?php
 
echo "Script 2"; //Using a Variable as 3rd parameter in the Explode function. Using FOR function.
 
$phrase = "Please don't blow me to pieces.";
$words = explode(" ", $phrase);
print_r($words);
 
for($i = 0; $i < count($words); $i++){
echo "Piece $i = $words[$i] <br />";
}
 
$explode_limit = 4;
$words = explode(" ", $phrase, $explode_limit);
for($i = 0; $i < $explode_limit; $i++){
echo "Piece $i = $words[$i] <br />";
}
 
?>
Edited by phpsane

Share this post


Link to post
Share on other sites

The only difference that matters here between a variable and a constant is that a variable can be modified and a constant cannot. If something does not need to modify a value then it doesn't matter which one you use.

 

Ok. I got my answer.

Thanks!

Share this post


Link to post
Share on other sites

By the way, 4 is a literal. A constant is something different. Once again we're back to the subject of you using correct terminology.

Share this post


Link to post
Share on other sites

I did not come across anything that states the 3rd parameter (explode limit) in the Explode function has to be a string or a constant.

Because nothing does. Learn HOW to read the manual and this becomes clear.

 

array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

That says explode() expects 2 or 3 parameters. The third parameter is optional, since it's enclosed in brackets.

 

That also tells you that IF you pass a 'limit' parameter, it must be something that evaluates to an integer. IF you do not pass a value, then the limit will be set to the default value contained in the constant PHP_INT_MAX (9223372036854775807).

 

All of these produce the SAME EXACT result from explode():

<?php

$text = "This is a simple sentence to explode into parts";

define('LIMIT_CONSTANT', 3);
$limit_string = '3';
$limit_int = 3;

echo "Using Literal:\n";
var_dump(explode(' ', $text,3));

echo "Using Constant:\n";
var_dump(explode(' ', $text, LIMIT_CONSTANT));

echo "Using String Variable:\n";
var_dump(explode(' ', $text, $limit_string));

echo "Using Integer Variable:\n";
var_dump(explode(' ', $text, $limit_int));

?>
I don't know why I'm still helping you, though. I guess I hope that someone will come along that can actually learn and this may be useful to them. Edited by Sepodati
  • Like 1

Share this post


Link to post
Share on other sites

Because nothing does. Learn HOW to read the manual and this becomes clear.

 

 

That says explode() expects 2 or 3 parameters. The third parameter is optional, since it's enclosed in brackets.

 

That also tells you that IF you pass a 'limit' parameter, it must be something that evaluates to an integer. IF you do not pass a value, then the limit will be set to the default value contained in the constant PHP_INT_MAX (9223372036854775807).

 

All of these produce the SAME EXACT result from explode():

<?php

$text = "This is a simple sentence to explode into parts";

define('LIMIT_CONSTANT', 3);
$limit_string = '3';
$limit_int = 3;

echo "Using Literal:\n";
var_dump(explode(' ', $text,3));

echo "Using Constant:\n";
var_dump(explode(' ', $text, LIMIT_CONSTANT));

echo "Using String Variable:\n";
var_dump(explode(' ', $text, $limit_string));

echo "Using Integer Variable:\n";
var_dump(explode(' ', $text, $limit_int));

?>
I don't know why I'm still helping you, though. I guess I hope that someone will come along that can actually learn and this may be useful to them.

 

 

I noted your sample on my NotePad++ as a reference. :)

Never heard of "literal" before in php. Don't see any difference between it and the Constant.

 

You don't know why you are helping me ? It is because:

 

1. My questions are basic and are raised after falling in pitfalls that newbies usually fall in. You want the newbies to avoid the pitfalls (even if you have not). You want all this subconsciously. Though consciously, you are not aware of it. You ain't such a bad guy after-all. Now are you ?  Just play tough and rough. But deep inside your a care bear! Lol! ;)

2. I touched your soft part. Yes, I did. ;)

 

Thanks- for teaching how to read the manual!

Yeah, your inputs- w-ill be handy for future newbies! My threads are usually geared towards newbies' learnings. I tailor my questions so every newbie benefit from my questions and you peoples' answers. (But for some reason you don't like my threads and my basic questions and lose patience. :( . Not good!).

 

(@requinix, I'm having to edit all those "--------" by deleting them before posting. Updating driver online).

Edited by phpsane

Share this post


Link to post
Share on other sites

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.