I am sorry, but I am new to php

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
walker
Forum Newbie
Posts: 1
Joined: Thu Jun 06, 2013 2:36 pm

I am sorry, but I am new to php

Post 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}";
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

Your foreach() is using the variable $date, but in the loop you are accessing $data. Maybe that is a typo.
(#10850)
Post Reply