Post by Llama! on Sept 9, 2005 15:41:19 GMT
Welcome to PHP Basics 2.
What do I learn in this tut?
Database connection is verry easy. First we use mysql_connect();
Now we use mysql_select_db(); to select the database
Now fill it all in and you are connected
Now let's continue with the While Loop. A while loop loops through a block of code if and as long as a specified condition is true.
here is an example wich will count to 5. It keeps checking tell $i is 5 and then it stops
Now let's goto the do...while. The Do...While is nearly the same as While but slightly different. This example will do the same as the code above:
Now the WHILE is later then the DO so if you do <= it counts to 6 so u use only <
The for statement/loop is used when you know how many times you want to execute a statement or a list of statements.
The following code will echo "Hey there!" 5 times:
First it says what $i is. Then it says it has to stay below 5. Then it counts 1 extra all everytime till it reaches 5.
The foreach loops over the array given by the parameter. On each loop, the value of the current element is assigned to $value and the array pointer is advanced by one - so on the next loop, you'll be looking at the next element.
The following example demonstrates a loop that will print the values of the given array:
That was the tut! Hope you understand and good luck
What do I learn in this tut?
- Database Connection
- While
- Do...While
- For
- Foreach
Database connection is verry easy. First we use mysql_connect();
<?php
$con = mysql_connect("localhost", "db user", "db pass");
?>
Now we use mysql_select_db(); to select the database
<?php
$con = mysql_connect("localhost", "db user", "db pass");
mysql_select_db("db name", $con);
?>
Now fill it all in and you are connected
Now let's continue with the While Loop. A while loop loops through a block of code if and as long as a specified condition is true.
here is an example wich will count to 5. It keeps checking tell $i is 5 and then it stops
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
Now let's goto the do...while. The Do...While is nearly the same as While but slightly different. This example will do the same as the code above:
<?php
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
Now the WHILE is later then the DO so if you do <= it counts to 6 so u use only <
The for statement/loop is used when you know how many times you want to execute a statement or a list of statements.
The following code will echo "Hey there!" 5 times:
<?php
for ($i=1; $i<=5; $i++)
{
echo "Hey there!<br>";
}
?>
First it says what $i is. Then it says it has to stay below 5. Then it counts 1 extra all everytime till it reaches 5.
The foreach loops over the array given by the parameter. On each loop, the value of the current element is assigned to $value and the array pointer is advanced by one - so on the next loop, you'll be looking at the next element.
The following example demonstrates a loop that will print the values of the given array:
<?php
$arr = array("one", "two", "three");
foreach($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
That was the tut! Hope you understand and good luck