Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need help stopping a variable from repeating.
Old 04-28-2010, 01:55 AM Need help stopping a variable from repeating.
grafitti's Avatar
Novice Talker

Posts: 9
Trades: 0
I'm trying to tell the script, If variable $shawn equals the same thing twice don't display the result. I'd appreciate if someone could help with me with this. Thanks.
Code:
<?php

$shawn = 'shawn';

$i = 0;

while($i < 5) {
	
	echo ''.$shawn++.'<br />';
	//if variable $shawn equals the same thing twice then do not display it.
	
	$i++;
	
}


?>
grafitti is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-28-2010, 02:35 AM Re: Need help stopping a variable from repeating.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
To answer your question:
PHP Code:
$last;

while(
$i 5) {
     if(
$shawn != $last)
          echo 
$shawn;

     
$last $shawn

There are a couple things I see that are wrong with your code. The first being that $shawn is set to a string so incrementing it doesn't make sense:
PHP Code:
$shawn 'shawn'//$shawn is a string
$shawn++; //increment $shawn doesn't make sense 
The other thing, while technically not 'wrong', is something I see constantly but I don't know why. There is no need to append empty strings to variables:
PHP Code:
echo ''.$shawn.'<br />'
is effectively identical to:
PHP Code:
echo $shawn '<br />'
I'm not sure why some people do that, but it is completely unnecessary.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-28-2010, 10:23 AM Re: Need help stopping a variable from repeating.
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
perhaps the people/resources these people go to for their instructions with PHP are teaching them to do it this way? That's the only thing I can think of.

kind of like some people favouring the if else statement(s) over switch statements, and others (over)utilizing switch when a simple if else will get the job done.
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE

Last edited by Lashtal; 04-28-2010 at 10:26 AM..
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 04-28-2010, 11:42 AM Re: Need help stopping a variable from repeating.
grafitti's Avatar
Novice Talker

Posts: 9
Trades: 0
Quote:
Originally Posted by NullPointer View Post
To answer your question:
PHP Code:
$last;

while(
$i 5) {
     if(
$shawn != $last)
          echo 
$shawn;

     
$last $shawn

There are a couple things I see that are wrong with your code. The first being that $shawn is set to a string so incrementing it doesn't make sense:
PHP Code:
$shawn 'shawn'//$shawn is a string
$shawn++; //increment $shawn doesn't make sense 
The other thing, while technically not 'wrong', is something I see constantly but I don't know why. There is no need to append empty strings to variables:
PHP Code:
echo ''.$shawn.'<br />'
is effectively identical to:
PHP Code:
echo $shawn '<br />'
I'm not sure why some people do that, but it is completely unnecessary.
Thanks bro works, Also the reason I put the ++ on the variable is because I wanted it to randomize the string I was confirming it wasn't just repeating but repeating the same value.
grafitti is offline
Reply With Quote
View Public Profile
 
Old 04-29-2010, 12:30 AM Re: Need help stopping a variable from repeating.
seventeen's Avatar
Novice Talker

Posts: 6
Trades: 0
<?php

$shawn = 'shawn';

$i = 0;

while($i < 5) {

if ($shawn != 'shawn'){

echo $shawn;
}

}


?>
__________________
Be Good Webmaster

Please login or register to view this content. Registration is FREE
seventeen is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help stopping a variable from repeating.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15436 seconds with 12 queries