Jump to content

ALL PHP pros or so called pros read.


Demonic

Recommended Posts

Is it or is it not true that a valid array can be called without single quotes inside a double quote array:

 

<?php
//Neel Basu.Temp.ERROR.Understanding
error_reporting(E_ALL);
$ary = array(
        "var1" => 'Val1',
        "var2" => 'Val2');
$ary['var1'] = "New Value";
echo "$ary[var2]";
?>

 

 

Now my friend neel_basu said thats an invalid way to code. echoing a variable like that it should be like this:

 

<?php
//Neel Basu.Temp.ERROR.Understanding
error_reporting(E_ALL);
$ary = array(
        "var1" => 'Val1',
        "var2" => 'Val2');
$ary['var1'] = "New Value";
echo "".$ary['var2']."";
?>

 

Now is it or is it not valid by doing both ways.  He don't believe me that this is a valid way to code and this is making me angry..

 

http://uni-code.com/codebox/arrayzz.php

 

 

Link to comment
https://forums.phpfreaks.com/topic/47240-all-php-pros-or-so-called-pros-read/
Share on other sites

How so if PHP doesn't echo out any errors when I tried my example?

 

How come when I do this: it throws an error?

 

<?php
//Neel Basu.Temp.ERROR.Understanding
error_reporting(E_ALL);
$ary = array(
        "var1" => 'Val1',
        "var2" => 'Val2');
$ary['var1'] = "New Value";
echo "$ary['var2']";
?>

 

This is ignoring..

Technically, your friend is correct. If you enable all error reporting, you will see the warnings issued when you don't quote the index of an array. When the index is not quoted, PHP will try to see if it is a Defined Constant, if it isn't, PHP will issue a Warning Message and then use the string value for the index, so the code will work.

 

BTW, if the reference is the only item being echoed, you don't need the double quotes at all:

<?php
echo $ary['var2'];
?>

 

Ken

you're both right. if you do it your way, it won't throw any errors, but your friend is saying that it isn't good coding practice. he's right about that. altho it is admirable testing the limits of php. let us know what else you find.

 

EDIT:

When the index is not quoted, PHP will try to see if it is a Defined Constant, if it isn't, PHP will issue a Warning Message and then use the string value for the index, so the code will work.

 

Ken

 

does this behavior differ between PHP4 and PHP5?

This

<?php
echo "$ary['var2']";
?>

is illegal and PHP will print an Error Message

 

This

<?php
echo "$ary[var2]";
?>

will cause PHP to produce a Warning Message, which you won't see unless you've turned on reporting of warning messages.

 

This

<?php
echo "{$ary['var2']}";
?>

and this

<?php
echo $ary['var2'];
?>

are legal.

 

Ken

we was referring to my code here for the select boxes :S http://uni-code.com/codebox/order.php

 

Is there any harm in what I've done?

 

<?php

//Connection include junk stufferz
$c = mysql_query("SELECT * FROM category");
$c2 = mysql_query("SELECT * FROM category");

if(!isset($_POST['update'])){

	echo("
		<form method='post'>
			<select name='mfy'>
		");
		while($ct = mysql_fetch_array($c)){
			echo ("<option value='$ct[order]'>$ct[name]</option>\n");
		}
		echo ("
			</select>
			<select name='order'>
				<option value='before'>Before</option>
				<option value='after'>After</option>
			</select>
		");
		echo ("
			<select name='cats'>
		");
		while($cd = mysql_fetch_array($c2)){
			echo ("<option value='$cd[order]'>$cd[name]</option>\n");
		}
		echo("
			</select><br />
			<input type='submit' value='Update Category' name='update' />
		</form>
	");

}else{

	if($_POST['order'] == "before"){
		$current = $_POST['mfy'];
		$before = $_POST['cats'];
	$idz = mysql_query("SELECT * FROM category ORDER BY `order` DESC");
	$boom = mysql_num_rows($idz);
	//echo $boom."<br />\n";

	$newO = ($before != 0) ? $before-1 : 0; 
	$new2 = $before+1;

	$orderz = mysql_query("UPDATE category SET `order` = '$newO' WHERE `order` = '$current' ") or die("ERROR");
	//$orderz2 = mysql_query("UPDATE category SET `order` = '$new2' WHERE `order` = '$before' ") or die("ERROR");

	while($id2 = mysql_fetch_array($idz)){

		if($id2[order] > $before || $id2[order] == $current){
			continue;
		}
		$order1 = $id2[order]+1;
		$order2 = $id2[id];
		mysql_query("UPDATE category SET `order` = '$order1' WHERE `id` = '$order2' ") or die("ERROR2");
	}

	}

}

echo ("
<BR><BR><BR><BR><BR><BR><BR><BR>
=================================
<BR>
");
$bb = mysql_query("SELECT * FROM category ORDER BY `order` ASC");
while($bbb = mysql_fetch_array($bb)){
	echo ("$bbb[id] <b>:</b> $bbb[order] <b>:</b> $bbb[name]<br />\n");
}
echo ("<BR>
=================================
");



?>

This

<?php
echo "$ary['var2']";
?>

is illegal and PHP will print an Error Message

 

This

<?php
echo "$ary[var2]";
?>

will cause PHP to produce a Warning Message, which you won't see unless you've turned on reporting of warning messages.

 

This

<?php
echo "{$ary['var2']}";
?>

and this

<?php
echo $ary['var2'];
?>

are legal.

 

Ken

 

http://uni-code.com/codebox/arrayzz.php

 

your second example I used this:

 

<?php
//Neel Basu.Temp.ERROR.Understanding
error_reporting(E_ALL);
$ary = array(
        "var1" => 'Val1',
        "var2" => 'Val2');
$ary['var1'] = "New Value";
echo "$ary[var2]";
?>

 

I don't see any errors though

OK I am here and here Is The Real fact

-----------------------------------------

1. Always Learn something thats Universaly true Dont deppend on the

    forgivefullness of any Language. In other Languages Variables Must be Out of

    Quotes Even In php variables within Single Quotes are treated as Normal Text

2. Here When You Use Array keys / Indexes Without Quotes It Understands it as a

    CONSTANT that has been defined ABOVE and if(!defined('that_index_or_key'))

    [something like that] PHP Assumes it as a text.

3. As Constants cannot be in Quotes When Using echo "$ary[var2]"; PHP Is Quite

    Sure about it that var2 isn't a constantas its in Double Quote Thats Why You

    dont Need Quotes Over here. And Its not Showing Errors

4. Always Try to Code In this Style echo "Text1".$ary['key']."Text2";

5. Personaly Speaking I can't not do that type of Coding [Without Quotes] as

    I Make Libraries. And In development Stage You all should Use

    error_reporting(E_ALL);

This

Code:

<?php

echo "$ary[var2]";

?>

will cause PHP to produce a Warning Message, which you won't see unless you've turned on reporting of warning messages.

No It Will no Show any Errors As I told Above in point no. 3

Try this

<?php
$ary['var2'] = "Text";
error_reporting(E_ALL);
echo "$ary[var2]";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.