Page 1 of 1

I am sorry, but I am new to php

Posted: Thu Jun 06, 2013 2:42 pm
by walker
I am doing tutorials and trying to learn php. I am just curious why is code is not working? Thank you for any help


<?php
$person = array(
"first_name" => "Walker",
"last_name" => "Kinne",
"address" => "123 Main Street",
"city" => "Beverly Hills",
"state" => "CA",
"zip_code" => "90210"
);


foreach($person as $attribute => $date) {
$attr_nice = ucwords(str_replace(" _ ", " ", $attribute));
echo "{$attr_nice}: {$data}";
?>

Re: I am sorry, but I am new to php

Posted: Thu Jun 06, 2013 3:51 pm
by requinix
"Not working" meaning you get output like

Code: Select all

first_name: last_name: address: city: state: zip_code:
when you should be getting

Code: Select all

First Name: Walker
Last Name: Winne
Address: 123 Main Street
City: Beverly Hills
State: CA
Zip Code: 90210
?

Code: Select all

foreach($person as $attribute => $date) {
    $attr_nice = ucwords(str_replace(" _ ", " ", $attribute));
    echo "{$attr_nice}: {$data}";
1. You're trying to replace " _ ". That's a space followed by an underscore followed by another space.
2. There is no $data variable.
3. You aren't using any newline or line breaks. Use a <br/> if you're outputting to HTML or \n if you're outputting plain text.



Or that you don't get any output at all?

Code: Select all

foreach($person as $attribute => $date) {
    $attr_nice = ucwords(str_replace(" _ ", " ", $attribute));
    echo "{$attr_nice}: {$data}"; 
?>
There's no closing } for the foreach loop.

Re: I am sorry, but I am new to php

Posted: Fri Jun 07, 2013 3:11 pm
by Christopher
Your foreach() is using the variable $date, but in the loop you are accessing $data. Maybe that is a typo.