Skip to content

Instantly share code, notes, and snippets.

@ksnider ksnider/loops.php
Created Jan 14, 2014

Embed
What would you like to do?
<?php
// 80/20 PHP - Day Five
// http://api-demo.com/scripts/loops.php
/*
*
* The FOR loop is used when you know how many times you want to
* execute a statement or a block of statements.
*
* SYNTAX:
* for (initialization; condition; increment)
* {
* code to be executed;
* }
*
*/
/*
$a = 0;
$b = 0;
for( $i=0; $i<5; $i++ )
{
$a += 10;
$b += 5;
echo 'a='. $a . ' and b=' . $b . '<br/>';
}
echo '<br/>At the end of the loop a='. $a . ' and b=' . $b . '<br/><br/>';
*/
/*
*
* The WHILE loop will execute a block of code IF and as long as a test expression is true.
*
* If the test expression is true then the code block will be executed. After the code
* has executed the test expression will again be evaluated and the loop will continue
* until the test expression is found to be false.
*
*
* SYNTAX:
* while (condition)
* {
* code to be executed;
* }
*
*/
/*
$i = 0;
$num = 50;
while( $i < 10)
{
$num--;
$i++;
echo 'i='. $i . ' and num=' . $num . '<br/>';
}
echo 'Loop stopped at i = ' . $i . ' and num = ' . $num . '<br/><br/>';
*/
/*
*
* The DO ... WHILE loop will execute a block of
* code at least once - it then will repeat the loop as long as a condition
* is true.
*
*
* SYNTAX:
* do
* {
* code to be executed;
* } while (condition);
*
*/
/*
$i = 0;
do {
$i++;
echo 'i='. $i . '<br/>';
} while ( $i < 10 );
echo 'Loop stopped at i = ' . $i . '<br/><br/>';
*/
/*
*
* The FOREACH loop is used to loop through arrays. For each pass,
* the value of the current array element is assigned to $value and the
* array pointer is moved by one and in the next pass next element
* will be processed.
*
* This is the loop I use the most.
*
* SYNTAX:
* foreach (array as value)
* {
* code to be executed;
*
* }
*
*/
/*
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
echo 'Value is '.$value .'<br/> <br />';
}
*/
/******* BREAK ******************************************************************
*
* The break keyword is used to terminate the execution of a loop.
*
* The break statement is placed inside the statement block. When executed, the loop
* stops running and moves to the next statement after the loop
*
*************************************************************************************
*/
/*
$i = 0;
while( $i < 10)
{
$i++;
echo 'i='. $i . '<br/>';
if( $i == 3 )break;
}
echo 'Loop stopped at i = ' . $i . '<br/><br/>';
*/
/******* CONTINUE ****************************************************************
*
* The PHP continue keyword is used to halt the current iteration of a
* loop but it does not terminate the loop.
*
* Like the break statement, the continue statement is placed inside the
* statement block containing the code that the loop executes, preceded by a
* conditional test. For the pass encountering continue statement, rest of the
* loop code is skipped and next pass starts.
*
***********************************************************************************
*/
/*
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
if( $value == 3 )continue;
echo 'Value is '.$value .'<br/> <br />';
}
*/
/*
require("isdk.php");
$app = new iSDK;
if ($app->cfgCon("sandbox")) {
// Current date in Infusionsoft-friendly format
echo "You connected at " . $currentDate = date("Ymd\TH:i:s") . "<br/>";
$returnFields = array('Id','FirstName','LastName'); // simple array
$query = array('LastName' => 'Dawg'); // associative array
$contacts = $app->dsQuery("Contact",10,0,$query,$returnFields);
echo "<pre>";
print_r($contacts);
echo "</pre>";
echo 'ID: ' .$contacts[0]['Id'] . '<br/>';
echo 'First Name: ' .$contacts[0]['FirstName'] . '<br/>';
echo 'Last Name: ' .$contacts[0]['LastName'] . '<br/><br/>';
echo 'First Dog: ' .$contacts[0]['FirstName'] . '<br/>';
echo 'Second Dog: ' .$contacts[1]['FirstName'] . '<br/>';
echo 'Third Dog: ' .$contacts[2]['FirstName'] . '<br/><br/>';
foreach ($contacts as $value)
{
echo 'First Name: ' .$value['FirstName'] . '<br/>';
echo 'Id: ' .$value['Id'] . '<br/><br/>';
$contactId = $value['Id'];
$update = array('Nickname' => 'Watch Dog');
$conID = $app->updateCon($contactId, $update);
}
} else {
echo "Not Connected...";
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.