Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
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.
| |
|
Topic: simple php question |
|
Author |
|
sb.net
Joined: 22 Sep 2006 Posts: 120 Location: USA
|
Posted: Sun 29 Oct '06 2:39 Post subject: simple php question |
|
|
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
|
Posted: Sun 29 Oct '06 8:59 Post subject: |
|
|
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
|
Posted: Sun 29 Oct '06 15:13 Post subject: Re: simple php question |
|
|
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
|
Posted: Sun 29 Oct '06 16:36 Post subject: |
|
|
Thanks 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
|
Posted: Sun 29 Oct '06 17:25 Post subject: |
|
|
an array allways starts with 0 stars[0] will be the first |
|
Back to top |
|
Brian
Joined: 21 Oct 2005 Posts: 209 Location: Puyallup, WA USA
|
Posted: Sun 29 Oct '06 20:28 Post subject: |
|
|
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
|
Posted: Sun 29 Oct '06 20:54 Post subject: |
|
|
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 |
|
|
|
|
|
|