Jump to content

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


phpsane

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);
 
?>
Link to comment
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 />";
}
 
?>
Link to comment
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.
Link to comment
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).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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