Tag Archives: loops

Why is a `switch` considered a looping structure for the purposes of `continue`?

I just got bit by assuming the following:
foreach ($arr as $key => $value) {
  switch($key) {
    // ... some other cases
    default:
      continue;
      // ^== assumption: move on to the next iteration of the foreach
      //     actual PHP: treat
Continue reading
Tagged , , | Leave a comment

break; vs continue; vs return;

i downloaded a free newsletter written in php from hotscripts.com i updated a little the code to add new functionality and i saw something i don’t get in the code i see:
foreach() { ...
 if() ...
  break;
 elseif() ...
Continue reading
Tagged , | Leave a comment

automate sum in array

I will try to explain the problem that i have with this code. This script works well to 3 persons ($numRows = 3).
$z=0;
$i=0;
$x=0;

 do {
        $total[] = (
        ${'contaH'.$z}[$i+0]*$final[$x+0]+
        ${'contaH'.$z}[$i+1]*$final[$x+1]+
        ${'contaH'.$z}[$i+2]*$final[$x+2]
        );
        $z++;
    } while ($z<$numRows); //3
Continue reading
Tagged , , | Leave a comment

Foreach loop issues in php

Here is some code I have: (p just echos plus adds a newline)
foreach ($vanSteps as $k => $reqInfo)
{
    p($k);
    if ('van' == $k) { p('The key is the van, continue'); continue; }//continue if we reached the part of
Continue reading
Posted in Sin categoría | Tagged , , | Leave a comment

For … else loops / control of flow in PHP?

Python offers a for…else structure like this:
for value in i:
    print value
else:
    print 'i is empty'
What’s the nearest equivalent to this in PHP? Edit: see @Evpok’s comment below – the for…else doesn’t actually work as the… Continue reading
Tagged , , , | 1 Comment

Is there a way to simplify this case statement?

I have this PHP case statement
switch ($parts[count($parts) - 1]) {
    case 'restaurant_pos':
        include($_SERVER['DOCUMENT_ROOT'] . '/pages/restaurant_pos.php');
        break;
    case 'retail_pos':
    include($_SERVER['DOCUMENT_ROOT'] . '/pages/retail_pos.php');
        break;
    .....

}
Which works great but I have many many files (like 190) and I would… Continue reading
Tagged , , , | Leave a comment

Is PHP's include resource-expensive (particularly during iterations)?

Does PHP cache include requests? I was wondering how to clean up my code and I thought about using a bit more includes. Consider the following scheme.
[foreach answer] [include answer.tpl.php] [/foreach]
This would require to include answer.tpl.php hundreds… Continue reading
Tagged , , | Leave a comment

PHP: Set the value of a multidimensional associative array element using a path defined in a separate array

Ok so I have an array that holds the following elements:
$array['a']['b'][0]['c'];
$array['a']['b'][1]['c'];
$array['a']['d'][0]['c']['c'];
$array['b']['c'];
Then in a separate array, I have defined the path to these values:
$structure[0] = array('a','b','#','c');
$structure[1] = array('a','d','#','c','c');
$structure[2] = array('b','c');
Finally,… Continue reading
Tagged , , | 1 Comment

PHP Loop – expression/function causing serious delay

I was wondering if anybody could shed any light on this problem.. PHP 5.3.0 I have a loop, which is grabbing the contents of a CSV file (large, 200mb), handling the data, building a stack of variables for mysql inserts… Continue reading
Tagged , , | Leave a comment

Multiple index variables in PHP foreach loop

Is it possible to have a foreach loop in PHP with multiple “index” variables, akin to the following (which doesn’t use correct syntax)?
foreach ($courses as $course, $sections as $section)
If not, is there a good way to achieve… Continue reading
Tagged , , , | 5 Comments
3 pages