logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: simple php question
Author
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Sun 29 Oct '06 2:39    Post subject: simple php question Reply with quote

I am making a PHP script, and I can not figure this out.
Code:
<?php
$1star="<img src="images/1star.jpg">";
$2star="<img src="images/2star.jpg">";
echo $1star;
?>

What I am trying to do is that I want to put an image in when I type "$xstar." what am I doing wrong?
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Sun 29 Oct '06 8:59    Post subject: Reply with quote

Maybe try this:

Code:
<?php

$stars = array(
   '<img src="images/1star.jpg">',
   '<img src="images/2star.jpg">',
);

echo $stars[0];
echo $stars[1];

// go random
echo $stars[ rand( 0, ( count( $stars ) - 1 ) ) ];

?>


Don't start a variable name with a number. It is also much more efficient to use an array to hold the values, in my view.

But with your code you have obvious errors, see corrections below:

Code:
<?php
$star1="<img src=\"images/1star.jpg\">";
$star2="<img src=\"images/2star.jpg\">";
echo $star1;
?>


...or use single quotes to wrap the variables...

Code:
<?php
$star1='<img src="images/1star.jpg">';
$star2='<img src="images/2star.jpg">';
echo $star1;
?>


But I'd suggest you stick with arrays.

Just as an aside, if you use ' single quotes instead of " double quotes when ever possible, it in theory will reduce the work on the PHP parsing engine because it does not parse the contents in the ' ... ' single quotes to look for varialbes as in:

Code:
<?php

$a = 'bla bla bla';

echo "<p>This is the value of string a:  $a</p>";

echo '<p>This is the value of string a:  $a</p>';


Notice that the output is very different, in the second example it does not work to display the value of $a, instead it simply prints $a, it does not treat it like a variable.

I prefer to use single quotes in nearly all cases where I echo a line of text.

I will use even with variables as in:

Code:
$a = 'bla bla bla';

echo '<p>This is the value of string a:' . $a . '</p>';


Just my two cents.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Sun 29 Oct '06 15:13    Post subject: Re: simple php question Reply with quote

sb.net wrote:
I am making a PHP script, and I can not figure this out.
Code:
<?php
$1star="<img src="images/1star.jpg">";
$2star="<img src="images/2star.jpg">";
echo $1star;
?>

What I am trying to do is that I want to put an image in when I type "$xstar." what am I doing wrong?


There are two mistakes.
As Brian told: the thing with the quotes. $star1='<img src="images/1star.jpg">';

Secondly a String ($something) can't start with a number.

The difference " and ' in PHP in the pasrsing.

example:

$a='my house';
$b="I am living in $a";
$c='I am living in $a';

echo $b;
//will output I am living in my house

echo $c;
//will output I am living in $a
Back to top
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Sun 29 Oct '06 16:36    Post subject: Reply with quote

Thanks Very Happy it will work great.

EDIT: It works, but I get confused typing "<?php echo $stars[3];" to get 4 stars. Is there a way too tell it to do it so that when I type "<?php echo $stars[3];" it means 3 stars?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Sun 29 Oct '06 17:25    Post subject: Reply with quote

an array allways starts with 0 stars[0] will be the first Wink
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Sun 29 Oct '06 20:28    Post subject: Reply with quote

Yes, arrays do always start with 0, but you could sort of "force" it to start with 1 as in:

Code:
$stars = array(
   '1' => 'bla bla bla',
   '2' => 'bla bla bla bla',
   '3' => 'bla bla bla bla bla',
);


Rather than try to make PHP be the creation you want it to be, something I used to try to do too, just learn the system and use it as it was intended. This will require you to re-tool your approach, but you will get far better results in the end.
Back to top
sb.net



Joined: 22 Sep 2006
Posts: 120
Location: USA

PostPosted: Sun 29 Oct '06 20:54    Post subject: Reply with quote

Ok, I am learning PHP, so I am wondering a lot. I found that in HTML to use it how it was intended.

--Thanks
Back to top


Reply to topic   Topic: simple php question View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner