gameboyz Posted March 29, 2009 Share Posted March 29, 2009 1) For the echo function is it better to use <?php echo("hello"); ?> or <?php echo "Hello"; ?> ? And is there a difference with single/double quotation marks? Link to comment Share on other sites More sharing options...
Synook Posted March 29, 2009 Share Posted March 29, 2009 echo "Hello" (without quotes) is interpreted quicker. The contents of double quoted strings are interpreted, while strings with single quotes are printed verbatim. For example: $var = "Hello";echo "I say $var"; //echos "I say Hello"echo 'I say $var'; //echos "I say $var" Single quotes are interpreted quicker, but double quotes are quicker than concatenation. Link to comment Share on other sites More sharing options...
zppblood Posted March 29, 2009 Share Posted March 29, 2009 Double quotes go through the process of converting variables/special characters into their values, where if you were to use single quotes, it wouldn't convert them and just print the string as is. <?php $var='4'; echo "$var"?>This would output 4 <?php $var='4'; echo '$var';?>This would output $var Link to comment Share on other sites More sharing options...
gameboyz Posted March 29, 2009 Author Share Posted March 29, 2009 echo "Hello" (without quotes) is interpreted quicker. The contents of double quoted strings are interpreted, while strings with single quotes are printed verbatim. For example:$var = "Hello";echo "I say $var"; //echos "I say Hello"echo 'I say $var'; //echos "I say $var" Single quotes are interpreted quicker, but double quotes are quicker than concatenation. What'd you mean double quotes are quicker than concatenation?2) About form enctype (http://www.w3schools.com/tags/att_form_enctype.asp) what's the difference between the 3? I experimented with try it editor using the first two, each time using "#$%^&" as first and last name and the results are identical. Link to comment Share on other sites More sharing options...
jlhaslip Posted March 29, 2009 Share Posted March 29, 2009 $var = "Hello";echo 'I say' . $var; //echos "I say Hello" Link to comment Share on other sites More sharing options...
gameboyz Posted March 30, 2009 Author Share Posted March 30, 2009 2) About form enctype (http://www.w3schools.com/tags/att_form_enctype.asp) what's the difference between the 3? I experimented with try it editor using the first two, each time using "#$%^&" as first and last name and the results are identical. Edit: This is weird. I tried it out not using the try it editor with the script as followed: <?phpif (!isset($_REQUEST['submit'])) {?><html><head><title>PHP Labs</title></head><body><form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" enctype="application/x-www-form-urlencoded"> First name: <input type="text" name="fname" /><br /> Last name: <input type="text" name="lname" /><br /> <input type="submit" value="Submit" name="submit" /></form></body></html><?php}else {$fname=$_REQUEST["fname"];$lname=$_REQUEST["lname"];?><html><head><title>PHP Labs</title></head><body><textarea><?php echo "$fname $lname"; ?></textarea></body></html><?php}?> No matter what I use for enctype it is the same. However it differs from try it editor in the sense that spaces are still spaces and they won't be converted to "+". Link to comment Share on other sites More sharing options...
justsomeguy Posted March 31, 2009 Share Posted March 31, 2009 The only time I ever put an enctype on a form is if I'm using the form to do a file upload, where you need to put an enctype. Other than that situation, I leave it off. Link to comment Share on other sites More sharing options...
gameboyz Posted May 6, 2009 Author Share Posted May 6, 2009 Anything wrong? <?phpfunction writecookie($name,$value,$expire) {$expire=time()+$expire;setcookie($name,$value,$expire);}?><html><head><title>PHP Test Labs</title></head><body><?phpwritecookie('cookie','value',120);echo $_COOKIE['cookie'];?></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted May 6, 2009 Share Posted May 6, 2009 You can't read a cookie on the same page you set it. You also can't set a cookie after sending HTML, you need to do it before sending any output. Link to comment Share on other sites More sharing options...
gameboyz Posted May 7, 2009 Author Share Posted May 7, 2009 I posted here: http://w3schools.invisionzone.com/index.php?showtopic=24775 and someone agreed that PHP cookies are easier to handle than JS ones.However since cookies gotta be set before any output, is it wise to use Javascript to set cookies and PHP to read the cookies? Since JS cookies can be set by a trigger (eg onclick) and PHP cookie-reading can be done after starting the HTML output. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 7, 2009 Share Posted May 7, 2009 It's better to use PHP for everything, the user can disable Javascript. Sending headers before you output anything shouldn't be a problem if you structure your PHP code correctly. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.