/    Sign up×
Community /Pin to ProfileBookmark

Spelling matters…

In another thread,

[QUOTE=ShrineDesigns][code=php]@fwrite(…) (…) @fclose(…)[/code][/QUOTE]

what are the “@” signs for ? is it a better way to write php ? or are you just placing this sign here for the fun of it ? ๐Ÿ˜ฎ
And, another question if I may, is it better to use single quotes ($_POST[‘foo’]) or double quotes ($_POST[“bar”]) for the arrays ? both work, so this is quite a dilemma… does anyone know what php.net recommends ?

to post a comment
PHP

18 Comments(s) โ†ด

Copy linkTweet thisAlerts:
@agent_x91Aug 06.2005 โ€”ย I have no idea about the @, I've been confused by that too. As for the single/double quotes, single quotes are used if you don't want any formatting in the string, i.e. no escape characters, variables are not used, etc. I think. Don't quote me on that though.

As for the other problem, I'll be interested in the answer myself ?
Copy linkTweet thisAlerts:
@aznchong91Aug 06.2005 โ€”ย What my experience with @ is that in case there is an error, it gives you the opportunity to write your own error instead of having a very ugly 'geek'-language error so the user can email you about it. For the arrays and $_GET and $_POST and things like that, it is better to have $_GET['blah']. For mysql_query, it is better to have " but if you are going to add a variable inside, you put it in ' ' marks. A little clearer?
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 06.2005 โ€”ย All right, thanks !

(Well the "geek" language doesn't disturb me very much indeed...)
Copy linkTweet thisAlerts:
@NogDogAug 06.2005 โ€”ย Yes, "@" means to suppress error-reporting for that expression. It can be useful to suppress "ugly" error message on your page when things don't work as expected, but then it becomes incumbent on you to check return values nad such and write your own, more graceful error-handlers when those expressions fail. Otherwise, your page may simply output nothing or else some erroneous output without any indication that something went wrong in the code.

For instance:
[code=php]
<?php
$handle = @fopen("file.txt", "w")
if($handle)
{
$result = @fwrite($handle, $text_to_write_to_file);
if(! $result)
echo <<<EOD
<p class="error">There was an error attempting to save your data to a file.
Please contact the <a href="mailto:[email protected]">webmaster</a>
if you continue to have this problem.</p>
EOD;

}
}
else
{
echo <<<EOD
<p class="error">There was an error attempting to create a file for your data.
Please contact the <a href="mailto:[email protected]">webmaster</a>
if you continue to have this problem.</p>
EOD;

}
?>
[/code]
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 06.2005 โ€”ย Personally, I always found these messages highly useful fo debugging...

If ever you've got a split second to sacrifice for me, what is this "EOD" thing ? I am not sure I understand it fully, it seems to take place of <?php and ?> ... :o
Copy linkTweet thisAlerts:
@NogDogAug 06.2005 โ€”ย Personally, I always found these messages highly useful fo debugging...

If ever you've got a split second to sacrifice for me, what is this "EOD" thing ? I am not sure I understand it fully, it seems to take place of <?php and ?> ... :o[/QUOTE]

It's called the "heredoc" method of writing literal string values.

It is begun by "<<<" followed by a string of characters and a newline. It means, treat everything that follows as one string until you see the string after the "<<<" starting a line by itself and followed by a ";". It allows variable interpolation as if it were a double-quoted string. The letters "EOD" is just an acronym I inherited from somwhere or other for "End Of Data", but you could use "pointfiftyae" if you wanted to:
[code=php]
echo <<<pointfiftyae
<p>This is a test.
It is only a test.</p>
pointfiftyae;

[/code]

One nice thing about it is that line breaks you type in the source code within a heredoc string will be output as newlines - you con't have to mess with n characters.
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 06.2005 โ€”ย Wow. Impressive. Thank you ! ?
Copy linkTweet thisAlerts:
@NogDogAug 06.2005 โ€”ย Wow. Impressive. Thank you ! ?[/QUOTE]
Oh, and another nice thing is you can use both double and single quotes within the string without worrying about having to escape them with back-slashes:
[code=php]
$longString = <<<EOD
<p>This is a "test."
'It is only a test,' he said.
The result was: '$result'.
EOD;

echo $longString;
[/code]
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 06.2005 โ€”ย Oh, this is useful ! But unless I am mistaken, you can also do this:
[code=php]<?php
some_code();
?>
<html to parse />
<?php
end of the code
?>[/code]

which has the same advantages!

Oh, but for mails your option is better. you're lucky. ?
Copy linkTweet thisAlerts:
@NogDogAug 06.2005 โ€”ย For my personal coding style, I dislike using lots of <?php ?> tags and going in and out of PHP mode. I find that more confusing - but maybe that's just me. For extended chunks of pure HTML text (where "chunk" is some arbitrary number of lines that I choose at the moment ? ), then I'll switch back out of PHP mode, but I don't like to do it for just a few lines, and I find throwing in a [b]<?php echo $variable ?>[/b] within the middle of a chunk of plain HTML text disjointed-feeling.

But it's a personal taste thing, I guess. I just find it easier to read - and therefore debug - when I don't keep hopping back and forth between HTML and PHP.
Copy linkTweet thisAlerts:
@aznchong91Aug 06.2005 โ€”ย Yea, i think so too. For example, I wanted to create a login and if you had the wrong password, there would be a back link. I tried to do it with just using php but couldn't get it to work because I didn't know my echo had to have before the quotes. So I tried getting out of php mode and do it with html. It gave me a huge headache! (and a lot more lines to write)
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 06.2005 โ€”ย OK, why not... that's personal... but this new heredoc-ish way of thinking opens, without a doubt, many doors to the evolution of my coding style, as I have now more choices than get out of PHP or become Master Of Backslashes ?
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 06.2005 โ€”ย Although:
[code=php]<?php
if (empty($_POST['pass']) || $_POST['pass'] != "ur_pass") {
?>
<div>
Error. <a blabla...>back</a>
</div>

<?php
} else {
?>

<div>
Good ! welcome !
</div>

<?php
}
?>
[/code]

Isn't it beautiful and simple ? ?
Copy linkTweet thisAlerts:
@NogDogAug 06.2005 โ€”ย ...

Isn't it beautiful and simple ? ?[/QUOTE]

Nah, this is beautiful and simple:
[code=php]
<?php
$msg = (empty($_POST['pass']) || $_POST['pass'] != "ur_pass") ?
"Error. <a href='blabla...'>back</a>" :
"Good ! welcome !";
echo "<div>n$msgn</div>n";
?>
[/code]

? :p
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 06.2005 โ€”ย *mumble grumble* ok ok I admit my defeat...you busted me on this one.? This technique ((bla) ? bla1 : bla2) is quite nice, although I am not used to it (I should though).
Copy linkTweet thisAlerts:
@aznchong91Aug 07.2005 โ€”ย It's definitely a lot shorter as I mentioned before. It could be the difference between exceeding your webspace limit and having plenty of space to make more pages!
Copy linkTweet thisAlerts:
@Stephen_PhilbinAug 07.2005 โ€”ย For info on the alternative syntax, you can have a peek here: http://www.zend.com/manual/language.expressions.php

I avoid using it myself, but it's still very handy to know, for if someone else uses it and you need to read their code.
Copy linkTweet thisAlerts:
@pointfiftyaeauthorAug 07.2005 โ€”ย Interesting... and useful ! Thank you very much.
ร—

Success!

Help @pointfiftyae spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 โ€” BETA 5.10,
whats_new: community page,
up_next: more Davinciโ€ข003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciโ€ข003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...