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}";
?>
I am sorry, but I am new to php
Moderator: General Moderators
Re: I am sorry, but I am new to php
"Not working" meaning you get output like
when you should be getting
?
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?
There's no closing } for the foreach loop.
Code: Select all
first_name: last_name: address: city: state: zip_code:Code: Select all
First Name: Walker
Last Name: Winne
Address: 123 Main Street
City: Beverly Hills
State: CA
Zip Code: 90210Code: Select all
foreach($person as $attribute => $date) {
$attr_nice = ucwords(str_replace(" _ ", " ", $attribute));
echo "{$attr_nice}: {$data}";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}";
?>- 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
Your foreach() is using the variable $date, but in the loop you are accessing $data. Maybe that is a typo.
(#10850)