Forum Moderators: coopster

Message Too Old, No Replies

PHP beginner

cheaky

         

humpingdan

10:16 am on Apr 5, 2004 (gmt 0)

10+ Year Member



ok recently been taking part in some php classes,
ive been very busy with work and havent had time to do any of the excercise's. i was wondering if any of you geniuses could lend me a hand - i need a few simple php scripts,

1). script to print every even integer value between 1 and 20 inclusive,

2). use a for loop to print all integers betweeen 1 and 30 divisible by either 3 or 4. Modify the loop to count matches

b). use a while loop to print all numbers between 1 and 30 divisible by 3 and 4. Now midify the loop to count the matches.

3). Write a script to output a 10 line table where the rows alternate in color. Label each row with its number.

4). Write a script to display 8x8 chequered table, chess board!

if anyone could just complete one excercies i would be very greatful, ive just left it too late!

Thanks!

ukgimp

10:30 am on Apr 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:)

Come on Dan your having a laugh arnt you? :)

You could find most of what you need with Google, but the basis seems to be looping logic. These tutorials have some decent examples

[zorka.com...]

And this one, under looping covers a few of what you have to do
[w3schools.com...]

humpingdan

11:49 am on Apr 5, 2004 (gmt 0)

10+ Year Member




Ok fair enuff!

so this creates a list of all the numbers from 1 to 20! correct!

<?php
$i=1;
while($i<=20)
{
echo " . $i . "<br />";
$i++;
}
?>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
wonderful, no i need to do sumthing like this,

<?php

function OddOrEven($intNumber)
{
if ($intNumber % 2 == 0 )
{
echo "Your number is even<br>";
}
else
{
echo "Your number is odd<br>";
}
}
//test the function with 2 values 40 and 3
OddOrEven(1);
to...
OddOrEven(20);
?>

but without writing in all the numbers as OddorEven!

just a little help!

ikbenhet1

11:59 am on Apr 5, 2004 (gmt 0)

10+ Year Member



$number=1;

if ( ($number / 2) == trim($number /2) ) {
echo "it is even";
}
else
{
echo "it is not even";
}

humpingdan

12:07 pm on Apr 5, 2004 (gmt 0)

10+ Year Member



but how do i encoporate the count from 1 to 20 into that so it outputs all the even numbers between 1 and 20!?

thanks for the help though!

ikbenhet1

12:14 pm on Apr 5, 2004 (gmt 0)

10+ Year Member



for ($number=0; $number < 20; $number++){

if ( ($number / 2) == trim($number /2) ) {
echo $number." is even<br>";
}
else
{
//echo "it is not even";
}

}

humpingdan

12:34 pm on Apr 5, 2004 (gmt 0)

10+ Year Member



<?php
for ($number=0; $number < 20; $number++){

if ( ($number / 2) == trim($number /2) ) {
echo $number." is even<br>";
}
else
{
//echo "it is not even";
}

}

?>

i dont want to seem ungreatful, but this doesnt work it out puts all the numbers correctly but apparently there all even numbers and i dont understand where its going wrong?

humpingdan

1:35 pm on Apr 5, 2004 (gmt 0)

10+ Year Member



<?
for ( $i = 1 ; $i <= 20 ; $i ++ ) {
echo "$i is " , ( $i % 2? "odd" : "even" ) , "<br>\n";
}
?>

this lists values 1 to 20, but displays both odds and evens, how do i get rid of the odds
!?!?!?!

ukgimp

1:51 pm on Apr 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whack an IF statement.

<?
for ( $i = 1 ; $i <= 20 ; $i ++ ) {

if($i % 2) {
//do nothing
}
else
{
print "$i is even<br>";
}

}
?>

humpingdan

2:01 pm on Apr 5, 2004 (gmt 0)

10+ Year Member



big thanks to you two!

dont think im grasping all this php malarky,
furthur questions that are listed above seem impossible,

print all integers between 1 and 3 divisible by 3 or 4,
and count the matches! yeh right

and...

print number between 1 and 30 divisible by 3 and 4?

and what about....

alternating row colours?

drawing back board! ;)

ukgimp

2:10 pm on Apr 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Colours
[drewfoster.com...]

Basics of checker board with tables
[blinkinglights.org...]

The divisible by three make me think of something along the line of:

Divide by three, then do an if and see if the value is a whole number. If it is print out the vale, else do nothing. Them move onto the next value.

Look at what the functions do on the left here
[uk.php.net...]