Help a newb with functions.

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Tue Feb 13, 2018 12:48 pm

I am attempting to perform a project as follows below for a training class I am taking.


Variable named SingleFamilyHome
Assign it the value of 399500
Variable named SingleFamilyHome_Display
Use the number_format() function to format the number to 2 decimal and assign it to the SingleFamilyHome_Display variable
Print it to the screen. The output should look like:
The current median price of a single family home in Pleasanton, CA is $399,500.00 (don’t forget the dollar sign)

Use a loop to print out the numbers 1 – 150 on the screen and submit to the Homework_2 drop box

The loop was easy

/*
$number = 0;

while (++$number <= 150)
{
print "$number" . "<br>";

}
*/
for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
{
echo "the number " . $numbers1;
echo "<br>";
}

No biggie there but I am still totally Mind F'd by functions. Can someone here post a function that meets the description above AND explain how it works? Ill post all I have so far below:

$SingleFamilyHome = 399500;
echo number_format( $SingleFamilyHome, 2 );
$SingleFamilyHome_Display;

echo "<br>";

Feel free to make fun of me all you like ;)
Dan

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 720
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 13, 2018 1:31 pm

From what I see, you're doing alright.

You haven't been asked to write a function and also you have echoed the result of a function rather than assign it to SingleFamilyHome_Display before displaying it.

As you found out in your loops, when you output using echo, there is no carriage return or line feeds unless you put them in the output; also note; with echo you can using a comma separated list of outputs:

Code: Select all

echo 'I have no line feed', 'nor a space between what I am outputting ', 'unless I put a space in.';
echo ' Then, to start a new line,<br>I need to put the HTML tag in my output';
Print does not allow you to do this.

A function is simply a set of instructions which you can keep using without having to re-type the same instructions out. They also serve to make reading the code easier if you name them well.

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Tue Feb 13, 2018 1:43 pm

but how do i output the results of

Code: Select all

number_format( $SingleFamilyHome, 2 );
into this:

Code: Select all

$SingleFamilyHome_Display;
Dan

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 720
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 13, 2018 1:55 pm

It's not outputting, it's assigning:

Code: Select all

$SingleFamilyHome_Display = number_format( $SingleFamilyHome, 2 );
Functions can return a value, number_format returns the value supplied once it's formatted it for you.

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Tue Feb 13, 2018 2:02 pm

oh i see, then after the value is set to the variable i just echo the variable and that's it. Thanks man!
Dan

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 720
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 13, 2018 2:05 pm

Yap, you got it :)

Post Reply