Forum Moderators: coopster

Message Too Old, No Replies

good way to use php shorthand?

good way to use php shorthand?

         

drooh

5:53 am on Nov 22, 2008 (gmt 0)

10+ Year Member



I am wondering what various ways there are to accomplish this

<?
$email = NULL;
$er_email = NULL;
if(isset($_POST['sub'])){
$email = $_POST['email'];
if(!$email){
$er_email = "<i>email is required</i>";
}
}
?>
<form action="test.php" method="post">
Email: <?=$er_email?><br />
<input type="text" name="email" value="<?=$email?>" /><br />
<input type="submit" name="sub" value="submit">
</form>


Specifically what does this if(!$email) mean?

Does it mean the same as if($email == "")

I just learned this shorthand and recently the
<?=$email?> is the same as <?php echo $email; ?>

What other nifty shorthand exists? Ive looked around but now found much on this.

drooh

6:25 am on Nov 22, 2008 (gmt 0)

10+ Year Member



here is a cool one

<?php if( condition ): ?>

<?php else: ?>

<?php endif; ?>

Anyango

8:11 am on Nov 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this cool if-else ;)

<?
$c=1;

$c==1? $v="ONE" : $v="Unknown";

echo $v;
?>

drooh

8:24 am on Nov 22, 2008 (gmt 0)

10+ Year Member



awesome! so something like this...

$email = $_POST['email'];
!$email ? $er_email = "<i>email is required</i>" : null;

Anyango

9:07 am on Nov 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this ;)

Normal (Generally Used version)
----------------------------------
$id=$_GET['id'];
$sql="select * from myTable where id=$id";
$result=mysql_query($sql) or die(mysql_error());
$record=mysql_fetch_assoc($result);
----------------------------------

Quick Version ;)

---------------------
$record=mysql_fetch_assoc(mysql_query("SELECT * FROM myTable WHERE id='".$_GET["id"]."'"));
---------------------

Oh Sorry :( i forgot the main question was related to what you asked, i got carried away by


What other nifty shorthand exists? Ive looked around but now found much on this.

[edited by: Anyango at 9:14 am (utc) on Nov. 22, 2008]

vincevincevince

10:42 am on Nov 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm going to caution you against too much fancy shorthand. Unless you are working for a firm producing ultra-high performance code for high traffic websites, the biggest effect of your shorthand will be to make it harder to maintain your code.

In the long run you will do best to write your code in the clearest way possible; and that is seldom by switching in and out of PHP parsing mode. If you have lots of HTML content in your script, move it out and put it into a template file or a database - separate your code and your content. Such practices are, in fact, the ultimate shorthand.

Anyango

10:45 am on Nov 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



vincevincevince is totally right! agreed!

Pico_Train

12:52 pm on Nov 22, 2008 (gmt 0)

10+ Year Member



I agree with VinceVInceVince.

I have one page of logic doing the php and I assign my variables I need to my template, the template with the html is used with light php to spit out what I want it to, multiple models with all my queries organised by action - select, delete, insert, update. You can even go so far as to add more models as you go along for specific sections of a site too. This makes it much easier to maintain.

Everything is split up, sure lots of clicking between folders but you always know where the problem is straight away this way as well. Is it your logic? Template? Or Query?

drooh

5:33 am on Nov 23, 2008 (gmt 0)

10+ Year Member



Yeah, as I was learning these it came to mind that portability may be diminished.

But none the less for my own knowledge Id like to know what is out there and what exists so than if I encounter it I will know how to understand it.