Problem with my code?

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Problem with my code?

Post by STUDIOny on Thu Feb 12, 2009 10:55 am
([msg=17740]see Problem with my code?[/msg])

Well, basically I'm starting to grasp the basics of PHP. I was thinking, oh, so far this is simple enough.
Then This hits me in the face :cry:

***

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/a6853085/public_html/Index.php on line 16

***

Basically this is what I'm trying to do:

tml>
<head>
<title>My first PHP site</title>
<body>

<?php

$variable = 'This is a variable';
$page = 1;

$array = array(
'first' => 'First Value',
'second' => 'Second Value',
'third' => 'Third Value', );

echo '<p>$array['first']</p>';
echo '<p>$array['second']</p>';
echo '<p>$array['third']</p>';

echo "<h1>$variable</h1>";

echo "<p>Welcome to my first PHP site! Page number $page</p>";

?>

</body>
</head>
</html>

***

Yes, the output makes no sense as such, but I was just testing variables and array's.

The exact problem I faced was when using the <p> </p> tags.

I had the arrays as:

$array = array(
'First Value',
'Second Value',
'Third Value', );

echo "<p>$array[0]</p>";
echo "<p>$array[1]</p>";
echo "<p>$array[2]</p>";

And then when i started using Associative Arrays (The code top of page), I started getting errors. I took the <p> tags out and it was fine, so I know they are the source of the problem.

If anyone could tell me what I am doing wrong, I would appreciate it very much.

Cheers.
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: Problem with my code?

Post by AgentSmithers on Thu Feb 12, 2009 1:03 pm
([msg=17761]see Re: Problem with my code?[/msg])

I belive you issue is here

'third' => 'Third Value', ); <--- is that a Trailing Comma? Seems like your loading up the next line into the array thats why its like what?! I want a T-String not an Array

-- Thu Feb 12, 2009 1:05 pm --

And Another thing I spotted

change

echo '<p>$array['first']</p>';
echo '<p>$array['second']</p>';
echo '<p>$array['third']</p>';

to

echo "<p>$array['first']</p>";
echo "<p>$array['second']</p>";
echo "<p>$array['third']</p>";

your grooping

<p>$array[ as the first part of the string when you use Start Single Quote and End Single Quote!

Cheers =)
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
AgentSmithers
New User
New User
 
Posts: 23
Joined: Thu Feb 12, 2009 12:27 pm
Location: Palm Springs
Blog: View Blog (0)


Re: Problem with my code?

Post by STUDIOny on Thu Feb 12, 2009 1:34 pm
([msg=17767]see Re: Problem with my code?[/msg])

Whoops, yes, I know about when to use the ' and " correctly, that was a stupid error.

One stupid question tho, what do you mean by a trailing comma?

Cheers for the post.

EDIT:

Sorted it myself with some trial and error basically.

I tried using
echo "<p>$array['first']</p>";
echo "<p>$array['second']</p>";
echo "<p>$array['third']</p>";
like you told me, but that didn't work.

The problem was in fact the ['first'] etc...
I took out the ' and changed them to [first] etc, and voila, problem solved.

Its strange tho, because the website I was using to learn this stuff, states that you use the ' ', when in fact they work without, and not with.
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)


Re: Problem with my code?

Post by AgentSmithers on Thu Feb 12, 2009 7:53 pm
([msg=17793]see Re: Problem with my code?[/msg])

If your executing an Array you need to use {} to evaluate

"<p>{$arrayvar['whatever']}</p>"
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
AgentSmithers
New User
New User
 
Posts: 23
Joined: Thu Feb 12, 2009 12:27 pm
Location: Palm Springs
Blog: View Blog (0)


Re: Problem with my code?

Post by STUDIOny on Fri Feb 13, 2009 5:47 am
([msg=17809]see Re: Problem with my code?[/msg])

Then how come I get this as an output when I use your code:

{

First Value}
{

Second Value}
{

Third Value}

***

The code I posted seems to work, so im sticking with that :)
STUDIOny
Experienced User
Experienced User
 
Posts: 73
Joined: Thu Feb 05, 2009 7:40 pm
Location: Newtownards, Northern Ireland
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests