Welcome to WebmasterWorld Guest from 54.167.155.147

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Simple PHP echo code not working

   
11:08 pm on Jun 17, 2014 (gmt 0)

5+ Year Member



I don't understand what im doin wrong i put this on my code

<?php

$couleurproduit = $_POST['couleurproduit'];
$tailleproduit = $_POST['tailleproduit'];

$zaza = $couleurproduit . " " . $tailleproduit;

echo $zaza ; ?>


it work great at this place

but i want to add it here =>

// Display line items
<?php
foreach($this->get_contents() as $item){
echo tab(5) . "<tr>\n";
echo tab(6) . "<td class='jcart-item-qty'>\n";
echo tab(7) . "<input name='jcartItemId[]' type='hidden' value='{$item['id']}' />\n";
echo tab(7) . "<input id='jcartItemQty-{$item['id']}' name='jcartItemQty[]' size='2' type='text' value='{$item['qty']}' />\n";
echo tab(6) . "</td>\n";
echo tab(6) . "<td class='jcart-item-name'> \n";

if ($item['url']) {
echo tab(7) . "<a href='{$item['url']}'>{$item['name']}</a> \n";
}
else {
echo tab(7) . $item['name'] . " \n";

}
echo tab(7) . "<input name='jcartItemName[]' type='hidden' value='{$item['name']}' /> \n";
echo tab(7) . "$zaza"; // <= HERE I TRY BUT IT SHOW NOTHING ?
echo tab(6) . "</td>\n";
echo tab(6) . "<td class='jcart-item-price'>\n";
echo tab(7) . "<span>$currencySymbol" . number_format($item['subtotal'], $priceFormat['decimals'], $priceFormat['dec_point'], $priceFormat['thousands_sep']) . "</span><input name='jcartItemPrice[]' type='hidden' value='{$item['price']}' />\n";
echo tab(7) . "<a class='jcart-remove' href='?jcartRemove={$item['id']}'>{$config['text']['removeLink']}</a>\n";
echo tab(6) . "</td>\n";
echo tab(5) . "</tr>\n";
?>



my code is much bigger is just the part where i want to put the echo $zaza
if i echo text it work but not my variable :(
thanks in advance
6:03 am on Jun 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



turn on error reporting
i think you will get an undefined index error
where do you define $zaza on this page?
9:10 am on Jun 18, 2014 (gmt 0)

5+ Year Member



Hello,

I have no error in reporting just at this place the echo don't work

Here my full code

at line 8 it's working

but at line 537 i try many times and no success

maybe at line 52 i should ad something

[edited by: jatar_k at 4:14 pm (utc) on Jun 20, 2014]
[edit reason] way too much code [/edit]

1:08 pm on Jun 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the first example, you write:

echo $zaza ;


In the second example, you write:

echo tab(7) . "$zaza";


Shouldn't it be...

echo tab(7) . $zaza;




<added>

Ah... no.

Because everything between double-quotes is evaluated.

My mistake - I almost never use double-quotes in PHP, so I forgot this.

Ignore me.

</added>
1:49 pm on Jun 18, 2014 (gmt 0)

5+ Year Member



strange that i cannot make it work
2:40 pm on Jun 18, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



The variable is in the global scope?

Try:
echo tab(7) . "[START]$zaza[END]";


Then view the source of the page and search for "[START]" - can you find anything?

Aside... you are better off formatting your PHP source code, rather than trying to pretty print the HTML output.
2:20 pm on Jun 19, 2014 (gmt 0)

5+ Year Member



the result output this => [START]

it echo only the text but not the variable....
9:29 pm on Jun 23, 2014 (gmt 0)

5+ Year Member



can't find the way to fix this problem crazy :(
10:20 pm on Jun 23, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



echo tab(7) . "[START]$zaza[END]";

What's the difference between this and
echo tab(7) . "[START]" . $zaza . "[END]"; 

?

That's a serious question from someone who doesn't speak php and reads most issues in this subforum as "Been there. Done that". Square brackets give me the willies. I never know when they mean something and when they're just cigars. Er, brackets.
11:50 pm on Jun 23, 2014 (gmt 0)

5+ Year Member



@Lucy

Square brackets are just text put inside double quotation marks. $zaza is a variable. When variable is inside double quotes it outputs the same way as your second example.

@OP

Your foreach misses "}". I hope it there in the original code.
9:40 am on Jun 24, 2014 (gmt 0)

5+ Year Member



In fact i just want to simply echo the variable $zaza but it's not working only text can be showed at this place don't understand why
12:52 pm on Jun 24, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



omoutop: turn on error reporting


maxweels: I have no error in reporting...


Just to clarify... did you enable full error reporting? For example:
error_reporting(E_ALL | E_STRICT);


This might give you some important clues. eg. If the variable is going out of scope and the variable is "undefined". (If E_NOTICE messages are not being reported then nothing will be output.)

There really is only two things that could be happening, either:

1. The variable is out of scope and is undefined (eg. using a global variable inside a function without first declaring it as global).

2. The variable is being overwritten.

You can check for #1 by enabling full error reporting. You can check for #2 by doing a search on your current code base for where you reference $zaza.

matrix_jan: Your foreach misses "}". I hope it there in the original code.


From the enormous code dump (which has since been cut from the thread), the curly brace did seem to be closed. $zaza did look as if it was defined and used in the global scope. However, it was an "enormous" code dump with much code in the global scope so it's difficult to say whether it was error free.

@lucy24: yes, as matrix_jan says, the result of those two statements is the same. In this particular case it just comes down to readability and personal preference. (String concatenation of single-quoted strings is marginally more efficient than double-quoted strings and "variable parsing" - but it's a micro optimisation.)
1:06 pm on Jun 24, 2014 (gmt 0)

5+ Year Member



ok i enable it and now see error

Notice: Undefined variable: zaza in /jcart.php on line 538
[START]


line 538 =>

echo tab(7) . "[START]$zaza[END]"; // Here where i try to put the echo $zaza //


so i need to declare it as global ?
2:42 pm on Jun 24, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



so i need to declare it as global ?


Yes, I think so. Looking back at your previous (unformatted) code dump it would seem that that bit of code was actually part of the display_cart() method inside the class (not part of the global scope as I previously thought). $zaza would seem to be declared in the global scope, so you will need the following at the start of the display_cart() method:

global $zaza;


Otherwise you are referring to a local $zaza variable which doesn't exist.

Also... I've just noticed that your tab() function is defined *INSIDE* the display_cart() method! Nested functions are just bad in PHP - they aren't really supported. In PHP, all functions are declared in the global scope. When a function is nested like this, the nested function is only declared when the parent function/method (display_cart) is called. The problem is if display_cart() is ever called a 2nd time. In which case PHP will attempt to redeclare the tab() function and you will get a fatal runtime error, "Cannot redeclare tab()...."
2:24 pm on Jun 27, 2014 (gmt 0)

5+ Year Member



thanks it work :)