/    Sign up×
Community /Pin to ProfileBookmark

array/loop question

Hi People.
I’m having some trouble with a script I made. I’m quite new to arrays within PHP, so I wonder if someone could tell me what’s wrong with my script.

The code at the bottom, displays:

[code]ArrayArrayArrayArrayArrayArray[/code]

What i THINK it should be displaying, is:

[code]123456[/code]

Eventually, I’ll replace the ‘echo’ with a ping script, and the 1,2,3 with URLs. (just so you know what I’m aiming to do eventually).

[code=php]<?php

$variable[] = array (“1”, “2”, “3”, “4”, “5”, “6”);

for (

$counter=1;
$counter<6;
$counter++)

{

echo $variable;

}

?>
[/code]

If anyone can tell me where the code is wrong, that would be very helpful, thanks!
?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@fredmvFeb 23.2004 — It's because you're telling it to print [font=courier]$variable[/font] &#8212; the literal array object &#8212; not one of the pieces of data from one of the elements in the array. In order to walk through the array and get the results you're looking for, you must change this line:echo $variable; To this:echo $variable[color=blue][$counter][/color];This would work because we're now accessing the current array element's data based on the value of the counter for the loop.
Copy linkTweet thisAlerts:
@hammerslaneauthorFeb 23.2004 — thanks a million fredmv!

i've spent an ungodly amount of time trying to get that to work...

$variable[] also had to be changed to $variable

thanks again!

[size=1]pp#200[/size]
Copy linkTweet thisAlerts:
@fredmvFeb 23.2004 — [i]Originally posted by hammerslane [/i]

[B]thanks a million fredmv![/B][/QUOTE]
No problem.[i]Originally posted by hammerslane [/i]

[B]$variable[] also had to be changed to $variable[/B][/QUOTE]
Yeah; I wasn't so sure about that &#8212; it's valid in Java (yes &#8212; Java &#8212; not Java[i]Script[/i]) so I was questioning it but then again, I don't code PHP so I'm not really all that great with it. It's just arrays, loops, variables and the like apply to generally all programming languages hence why I was able to help you out quite easily.
Copy linkTweet thisAlerts:
@hammerslaneauthorFeb 23.2004 — What I'm about to ask here, is technically a new thread, but I feel greedy starting 2 threads in 1 day :p

The code below, is a function that's being used in my array/while loop above (that fredmv helped me with).

It's a function that should open a URL, and if the URL ($link) connects, it assigns 'connected' text to $mesage, and if it doesn't connect, it assigns some 'fail' text to $message.

The error I'm getting though when using this chkuri function, is:Fatal error: Cannot redeclare chkuri() (previously declared in \Maze01SharedWebintranetpingpingfo.php:46) in \Maze01SharedWebintranetpingpingfo.php on line 46
Line 46 is the first line of PHP below...
[code=php]<?

function chkuri($link){
$churl = @fopen("http://".$link,'r');
if (!$churl) {
$message="http://$link is down";
}else{
$message="Connected to http://$link";
}
return $message;
}
?>
[/code]
I'm not sure why it's returning the odd error message, I've never seen it before. Maybe someone can give me an alteration to the code...

Thanks people
×

Success!

Help @hammerslane spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...