parameter list and their problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
somegone
Forum Newbie
Posts: 3
Joined: Fri Jul 26, 2002 8:39 am

parameter list and their problems

Post by somegone »

i wanted to write an alias for the printf function which automaticly puts a br and a newline on the end of the line.
either it has to work like this:
<code>
function my_printf () {
printf (
do_something_important(func_get_args())
);
echo "<br>\n";
}

// so the line:
my_printf ("Hello World. Today it's the %d.!", (int)date("d"));
// will have the output line:
// Hello World. Today it's the 25.!<br>\n

</code>

but i have no idea how to convert the argument-array to use it again as parameter-list for the printf function.
so the do_something_important function has to split the array in the single arguments and put it in the right format ...
i know that this would work in C because i've tried it. but i wonder how it works in php

or it hast to work with a kind of makro ... i look forward to any kind of solution

second question:
is there any algorythm to calculate the week number out of a date?

thanks and sorry for my bad english =)
bye
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might want to take a look at func_num_args, func_get_arg and especially func_get_args

and for the second question getdate ;)
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

He already knows how to get the arguments, but the problem is passing those arguments to printf().

Here's a neat trick. Use eval(), like this:

Code: Select all

<?php

function my_printf()
&#123;
	$args = func_get_args();
	$num_args = count($args);

	$arg_list = '';
	for ($x = 0; $x < $num_args; $x++)
	&#123;
		if ($x) $arg_list .= ', ';
		$arg_list .= "\$args&#1111;$x]";
	&#125;

	eval("printf($arg_list);");

	echo "<br>\n";
&#125;

my_printf ("Hello World. Today it's the %d.!", date("w") + 1);

?>
All right! Post #100
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops, I'm sorry. Haven't read the post to the end (skipped the code-section) :oops:
somegone
Forum Newbie
Posts: 3
Joined: Fri Jul 26, 2002 8:39 am

Post by somegone »

ok thanks about the parameter list problem
but the date("W") is not the right solution for my problem because it returns the week number of the current week.
but what if i want to know the week number of the 15th of august in 1984?

so i need a function which gets a date as parameter and returns the week number

for an example:
$week_nr = date_week_nr ("1984-08-15");

week_nr will have the week number of the week where the 15th of august was in 1984 ...

hope you know what i mean :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Why don't you have a look at PHP's date and time functions:
http://www.php.net/manual/en/ref.datetime.php
and perhaps read about date() so that you can make it do what you want:
http://www.php.net/manual/en/function.date.php

Mac
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

Any date can be used if you pass a "timestamp" to the date() function:

Code: Select all

date("w", $timestamp)
If you have a date in the form "1984-08-15", use strtotime() to convert the string to a timestamp.

Code: Select all

date("w", strtotime("1984-08-15"))
(Note: A capitol 'W' won't work. It means "week number of year." Use a small case 'w')
Post Reply