phpsane Posted September 7, 2017 Share Posted September 7, 2017 (edited) 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 September 7, 2017 by phpsane Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted September 7, 2017 Solution Share Posted September 7, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
phpsane Posted September 7, 2017 Author Share Posted September 7, 2017 (edited) (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 September 7, 2017 by phpsane Quote Link to comment Share on other sites More sharing options...
phpsane Posted September 7, 2017 Author Share Posted September 7, 2017 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! Quote Link to comment Share on other sites More sharing options...
requinix Posted September 7, 2017 Share Posted September 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
Sepodati Posted September 7, 2017 Share Posted September 7, 2017 (edited) 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 September 7, 2017 by Sepodati 1 Quote Link to comment Share on other sites More sharing options...
phpsane Posted September 7, 2017 Author Share Posted September 7, 2017 (edited) 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 September 7, 2017 by phpsane Quote Link to comment Share on other sites More sharing options...
requinix Posted September 8, 2017 Share Posted September 8, 2017 Never heard of "literal" before in php. Don't see any difference between it and the Constant. http://www.webreference.com/programming/php/by_example/index.html You don't know why you are helping me ?I'm doing it because fewer and fewer people want to. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.