tags:

views:

102

answers:

3

What is the purpose of these lines:

<?php $title=($cfg && is_object($cfg))?$cfg->getTitle():'Apptitle :: My First App'; ?>

<?=Format::htmlchars($title)?>

Can someone explain the usage here?

I see the top line being used as first line in a php file, then the second line being used to populate the title.

Why this implementation? What is the purpose other than an object?

I guess the purpose maybe could be to reuse the object across the session. Unsure.

+7  A: 

Adding linebreaks and comments:

<?php
$title = $cfg && is_object($cfg)   // if $cfg isn't empty (false, 0, null) and it's an object
       ? $cfg->getTitle()  // then set $title to the return of getTitle()
       : 'Apptitle :: My First App'; // otherwise use this as a default
?>

<?=Format::htmlchars($title)?>  // this is a shortcut for echo. it probably escapes
                                // special characters: < becomes &lt; etc
                                // it doesn't change any values: it just echoes

Basically all it's doing is checking your $cfg object (whatever that is) to see if there's a title set - if not, it provides a default. It then prints it to the screen in a html-friendly way. Nothing to do with sessions or anything like that.

Another quick point: in your own code, you should avoid using the shortcut <?= since it's not very portable. That is, even though it might work on your testing server, your deployment site or someone else who wants to use your code might have it turned off. It's recommended to avoid it.

nickf
Thanks for your excellent answer, fantastic. One quick one. What does Format means on that line?
Codex73
A: 

This seems to be a generic template, which you can customize by providing a $cfg object to. In this short example if you provide a title for the current page, it is; otherwise a default one is printed.

Zed
A: 

The first line is checking to see if $cfg is a valid variable and if it is an object. If $cfg is a valid object, it sets the value of $title to the return value of $cfg->getTitle(), otherwise it sets $title to 'Apptitle :: My First App'.

The second line is then outputting the return value of the Format::htmlchars method which is passed the $title variable.

Adam Raney