Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Argument of native function untouchable?? 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys,

All my attempts to dynamically call STR_PAD_BOTH failed as you can see below.

Only the last version, the untouched one, works.

So, is it really impossible???

Code:
$string = "BOTH";

echo str_pad("some text", 25, "-", STR_PAD_{$string}) . "<br />";
// returns Parse error: parse error, unexpected '{'

echo str_pad("some text", 25, "-", STR_PAD_ . $string) . "<br />";
// returns ----------------some text

echo str_pad("some text", 25, "-", "STR_PAD_" . $string) . "<br />";
// returns ----------------some text

$string = "STR_PAD_BOTH";

echo str_pad("some text", 25, "-", $string) . "<br />";
// returns ----------------some text

echo str_pad("some text", 25, "-", STR_PAD_BOTH) . "<br />";
// returns --------some text-------- ( expected result )
 

I've tried that too :

Code:
$string = "BOTH";
echo eval("str_pad('some text', 25, '-', STR_PAD_" . $string . ");");
// returns nothing
 
That's because that paramter of stre_pad has to be an integer. In reality what you are passing to the funtion is the name of a constant that contains an integer.

So you can dop to things.

either pass the number 2 for "BOTH"

or do what you are doing using the constant() function to evaluate your string:

Code:
echo str_pad("some text", 25, "-", [red]contant("[/red]STR_PAD_[red]"[/red] . $string[red])[/red]) . "<br />";



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 

Thank you soooo much Vac :)

Works like a charm.
I had already heard of the constant() function before but I was too dumb to remember it for this particular case :(

Anyway, thanks again! :)
 
You're welcome, glad I could help.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top