logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 05-22-2015, 09:36 PM   #1
jacob1986
 
Join Date: May 2015
Posts: 20
Default php variables.

I have been trying to learn about php from a book but I'm stuck on one particular part. The example involves variables - I have completed the arrays section and one half of the variables example.

I've been told how to write separate variables in php (example one below), and all is fine (shows no errors). But when I write the second example (example two), it shows errors on the page? I will write the errors under the second example.

Example one:
<?php
$item1="milk";
$item2="fruit";
$item3="sugar";
$item4="tea";
$item5="chocolate";
?>

Example two:
<?php
echo $items[0]; //output first item (milk in this case)
echo $items[1]; //output second item (fruit in this case)
echo $items[2]; //output third item (sugar in this case)
echo $items[3]; //output fourth item (tea in this case)
echo $items[4]; //output fifth item (chocolate in this case)
?>

Errors:
Notice: Undefined variable: items in C:\xampp\htdocs\xxx.php on line 2
Notice: Undefined variable: items in C:\xampp\htdocs\xxx.php on line 3
Notice: Undefined variable: items in C:\xampp\htdocs\xxx.php on line 4
Notice: Undefined variable: items in C:\xampp\htdocs\xxx.php on line 5
Notice: Undefined variable: items in C:\xampp\htdocs\xxx.php on line 6
jacob1986 is offline   Reply With Quote
Old 05-22-2015, 09:48 PM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

Example 2 is trying to reference an array which you haven't defined
Code:
$items=array("milk","fruit","sugar","tea","chocolate")

or > php 5.4

$items=["milk","fruit","sugar","tea","chocolate"]
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas

Last edited by edbr; 05-25-2015 at 03:58 AM..
edbr is offline   Reply With Quote
Old 05-27-2015, 01:58 PM   #3
jacob1986
 
Join Date: May 2015
Posts: 20
Default

Hello edbr,
I fervently hope you you don't mind me asking another question regarding php - sorry but I'm really stuck using this book

I have tried to write some code (examples are from a book) but every time I run the code in my php file - I get an error message (this is for both codes) - the code(s) are below.

<?php
if (expression) {
//run this code block
}
?>
------------------------------------------------
<?php
if ($total_amount>=200){
$discount=0.15*$total_amount;
$total_amount=$total_amount-$discount;
}
echo"You need to pay the amount: ".$total_amount;
?>
jacob1986 is offline   Reply With Quote
Old 05-28-2015, 04:09 AM   #4
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

what errordo you get? did you declare it?
try $total_amount=100; see if you still get error

then try $total_amount=300; to fulfill the condition of greater than 200 to see it work
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas

Last edited by edbr; 05-28-2015 at 04:35 AM..
edbr is offline   Reply With Quote
Old 05-30-2015, 02:33 PM   #5
jacob1986
 
Join Date: May 2015
Posts: 20
Default

The code - if ($total_amount>=200){ was solved by missing the '>' symbol in the code i.e. if ($total_amount=200){

The other piece of code - if (expression){ I cannot seem to fix and repeatedly receive the message 'Notice: Use of undefined constant expression - assumed 'expression' in C:\xampp\htdocs\xxx.php on line 2'

Code:
<?php
if (expression) {
//run this code block
}
?>
jacob1986 is offline   Reply With Quote
Old 05-31-2015, 10:58 AM   #6
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

That's not valid code. Its just an illustration of the if condition structure.
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

Forum Jump


All times are GMT. The time now is 12:37 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com