Jump to content

Recommended Posts

Sorry for the stupid question folks, but here's some code of mine I'm testing with:

 

function test_getIndex_Of_Array()
{

$temp = array();

for ($i = 1; $i < 11; $i++) {
 $temp[$i - 1] = $i;
                             }

$count = sizeof($temp);

for ($i = 0; $i < $count; $i++) {
 echo $i . ' ' . $temp[$i] . '<br>';
                                   }
}

//////////////////////////////////////////////////////////////////////////////////

function test_ArraySum()
{

$temp = array();

for ($i = 1; $i < 11; $i++) {
 $temp[$i - 1] = $i;
                             }
$sum = array_sum($temp);
echo $sum;

}

 

the 2 functions are called *in order* from a separate <php> script above that. that script just has the calls in it. I'm getting the echo from the 1st but nothing for the second. What rule about PHP am I missing here?

 

There really needs to be a place on the web where rules about any IDE should be listed in the forms of enumerations so learning like this doesn't have to take place. After all, coding is nothing more than a bunch of rules...

 

thanks guys.

Link to comment
https://forums.phpfreaks.com/topic/272255-array_sum-returns-nothing/
Share on other sites

Well, for one the code you've posted is not even valid PHP code.  You're missing several closing brackets ( } ).  

 

Rather than echo, use var_dump for testing so you can get a better idea of what information is contained within a variable.  For instance if the variable contained false or null then echo would show you nothing, var_dump would show you which.

 

Copy and paste your actual code here if you want help.

 

You might have solved his problem - the reason why his code wasn't working is that his brackets were in the wrong place. A php file containing only the following should work:

<?php

 

$temp = array();

 

for ($i = 1; $i < 11; $i++) {

$temp[$i - 1] = $i;

}

$sum = array_sum($temp);

echo $sum;

 

?>

Well, for one the code you've posted is not even valid PHP code.  You're missing several closing brackets ( } ).  

You mean like the three that are indented way off to the side?

 

Code works for me. I get

0 1<br>1 2<br>2 3<br>3 4<br>4 5<br>5 6<br>6 7<br>7 8<br>8 9<br>9 10<br>55

You mean like the three that are indented way off to the side?

 

Code works for me. I get

0 1<br>1 2<br>2 3<br>3 4<br>4 5<br>5 6<br>6 7<br>7 8<br>8 9<br>9 10<br>55

 

You are right, req. The others may not be. I don't understand brackets in the wrong place. I don't see it. I only post code that isn't obvious (no PHP tags were posted, that's obvious). Copy from Dreamweaver was just not congruent. Meta was off. Big deal.

 

both code blocks works for you, req?? I only get echos for the first block. nothing is printed for the second. wonder what it is....ideas?

 

I will try each block by themselves later on. Sofware contends that the 2nd block alone will run. I'll get back to you on that.

Edited by help_me_with_php

all,

 

this function:

 

function test_ArraySum()
{

$temp = array();

for ($i = 1; $i < 11; $i++) {
$temp[$i - 1] = $i;
}
$sum = array_sum($temp);
echo $sum;

}

does NOT return anything for me still. but, I forgot to mention that this is testing on IIS 7.0 with PHP 5.3 I NEVER ever test non-MS stuff on MS-based machines for obvious reasons. Could this be it? This is the first time, literally, that I've never had a PHP function not work on a Microsoft IIS platform, if that really is the problem. I wouldn't doubt it, considering MS's terrible products. I realize that testing PHP on IIS is not wise and I wouldn't do it if I had an Apache option. But I don't for this run. Sorry.

 

thoughts? that's all I can assume. nothing else makes sense. all options exhausted. thanks guys.

Edited by help_me_with_php

Please don't troll us.

 

<html>
<?php error_reporting(-1); ini_set("display_errors", true); ?>
<body>
<h1>Today is <?php echo date("Y-m-d"); ?></h1>
<p><input type="button" value="<?php echo 'This is a button'; ?>" /></p>
<pre>
<?php

function test_getIndex_Of_Array()
{

$temp = array();

for ($i = 1; $i < 11; $i++) {
  $temp[$i - 1] = $i;
                                                          }

$count = sizeof($temp);

for ($i = 0; $i < $count; $i++) {
  echo $i . ' ' . $temp[$i] . '<br>';
                                                                        }
}

//////////////////////////////////////////////////////////////////////////////////

function test_ArraySum()
{

$temp = array();

for ($i = 1; $i < 11; $i++) {
  $temp[$i - 1] = $i;
                                                          }
$sum = array_sum($temp);
echo $sum;

}

test_getIndex_Of_Array();

?>
</pre>
<p><?php echo bin2hex(basename(__FILE__)); ?></p>
</body>
</html>

What does that output?

Edited by requinix

Please don't troll us.

 

 

Are you talking to me? I'm not a troll. I can't even remember what that is or what trolls do. Like a spammer, right?

 

Anyway,j I'm switching operating systems, so I'll let you know what the hex output looks like when I can test it. I will be a few hours if not more. thanks.

I cannot see a call to the second function in that code

you are right. man...sorry! Here's the output, and it now returns the sum. However, I'm not sure if this is relevant but my space on this shared server has now switched from Windows to Linux.

 

I sure hope that doesn't matter, but it's possible. Isn't it? thanks.

 

new result:

 

0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
55
74657374322e706870

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.