Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Learning php please help
Old 09-09-2010, 06:28 AM Learning php please help
Novice Talker

Posts: 11
Name: Robert Gouveia
Trades: 0
Hi everyone, I am just starting to learn php this morning, I have had only 2 hours lesson and my teacher has set me an assignment at the end of the lesson and asked me to:

Program to display the smallest and the largest number among three numbers

Can you tell me if my coding is correct.

PHP Code:
<?php

$a
=1;
$b=2;
$c=3;

if(
$b<>$a || $c)
{
echo 
$a."is less than".$b".$c."is more than".$b;
}

?>
If not can you tell me why this doesnt work and could you help me understand what will work.

Thanks in advance

RobbieG is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-09-2010, 06:56 AM Re: Learning php please help
Kelpie's Avatar
Skilled Talker

Posts: 82
Name: Andrew
Location: SW Scotland
Trades: 0
Have you been taught anything about arrays? If so, that would be a better way of storing the values
(BIG hint: whether you use an array or separate variables, your teacher has not asked you to display the middle value, just the min and max)

Last edited by Kelpie; 09-09-2010 at 07:07 AM..
Kelpie is offline
Reply With Quote
View Public Profile
 
Old 09-09-2010, 07:04 AM Re: Learning php please help
Novice Talker

Posts: 11
Name: Robert Gouveia
Trades: 0
no, i have just had 2 hours lesson and he told me just the very basics. I have not got to the arrays yet
RobbieG is offline
Reply With Quote
View Public Profile
 
Old 09-09-2010, 07:18 AM Re: Learning php please help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You have a couple of syntax mistakes in your code.

One way to find the largest and smallest values without using an array:
PHP Code:
$a=1
$b=2
$c=3;

$largest $a;
if(
$b $largest)
     
$largest $b;
if(
$c $largest)
     
$largest $c;

$smallest $a;
if(
$b $smallest)
     
$smallest $b;
if(
$c $smallest)
     
$smallest $c;

echo 
"Largest: $largest \n";
echo 
"Smallest: $smallest"
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-09-2010, 08:17 AM Re: Learning php please help
Novice Talker

Posts: 11
Name: Robert Gouveia
Trades: 0
I dont know if i should use something like this as my teacher will know its not my coding as her has not showed me the $largest \n yet.

Plus i only want to use code inwhich i understand 100% as if i give him work which i have not done its only me i am cheating as i will not understand what i have created...

What are the syntax errors and why. So i can understand not doing this in future.
Thanks for the help
RobbieG is offline
Reply With Quote
View Public Profile
 
Old 09-09-2010, 08:58 AM Re: Learning php please help
Novice Talker

Posts: 11
Name: Robert Gouveia
Trades: 0
Ok i have changed the coding.

Does this work?

PHP Code:
<?php

$a
=1;
$b=2;
$c=3;

if(
$b>=$a && $b<=$c)
{
echo 
$a"is less than".$b"." .$c"is more than" .$b;
}
else
{
echo 
"error within calculations";
}

?>
RobbieG is offline
Reply With Quote
View Public Profile
 
Old 09-09-2010, 10:37 AM Re: Learning php please help
Banned

Posts: 408
Name: mushget
Trades: 0
Quote:
Originally Posted by NullPointer View Post
You have a couple of syntax mistakes in your code.

One way to find the largest and smallest values without using an array:
PHP Code:
$a=1
$b=2
$c=3;
 
$largest $a;
if(
$b $largest)
     
$largest $b;
if(
$c $largest)
     
$largest $c;
 
$smallest $a;
if(
$b $smallest)
     
$smallest $b;
if(
$c $smallest)
     
$smallest $c;
 
echo 
"Largest: $largest \n";
echo 
"Smallest: $smallest"
This code is right. But if there are 100 or more numbers. it will be complex
mushget is offline
Reply With Quote
View Public Profile Visit mushget's homepage!
 
Old 09-09-2010, 10:53 AM Re: Learning php please help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by RobbieG View Post
Ok i have changed the coding.

Does this work?

PHP Code:
<?php

$a
=1;
$b=2;
$c=3;

if(
$b>=$a && $b<=$c)
{
echo 
$a"is less than".$b"." .$c"is more than" .$b;
}
else
{
echo 
"error within calculations";
}

?>
Your code is correct, but it won't give you the largest and smallest values. I don't mean to just give you the answer, but it is difficult to show you how without giving you a solution in the process. Another way to do it would be:
PHP Code:
if($a >= $b && $a >= $c)
     echo 
$a ' is the largest';
elseif(
$b >= $a && $b >= $c)
     echo 
$b ' is the largest';
else
     echo 
$c ' is the largest'
You could do something similar for finding the smallest.

By the way \n just means new line. The code
PHP Code:
echo "hello \n world"
will output
Code:
hello
world
Just make sure you use double quotes when doing this otherwise you'll get the literal \n.

Quote:
Originally Posted by mushget View Post
This code is right. But if there are 100 or more numbers. it will be complex
Hopefully if you had that many numbers they would be in an array.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-09-2010, 02:36 PM Re: Learning php please help
Novice Talker

Posts: 13
Trades: 0
I would advise you to check out youtube there is some great vids to help you learn php i learnt alot from it
__________________

Please login or register to view this content. Registration is FREE
services ||
Please login or register to view this content. Registration is FREE
T-H-C is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Learning php please help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28897 seconds with 12 queries