Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another daft question

Status
Not open for further replies.

Elmserv

Programmer
Sep 25, 2004
72
GB
Most of my experience is in FoxPro & I have a fairly rigid "style".

Writing in a mixture of PHP & HTML looks a bit messy to my untrained eye.

At the moment I have stuck rigidly to PHP using the Print function, what are the norms.

I don't like the "{}" as it is quite difficult to check if all are closed & I had to spend a fair bit of time the other day debugging 40 lines to make sure they were all closed in the right places -- is there an alternative?


Richard
 
You can use several methods of outputting text. The most common being [blue]"echo"[/blue].

Code:
echo "Some html here"; //[green]Surround html by double quotes.[/green]

You can also use the HEREDOC syntax.

echo <<<END

Lost of html here

more html

END;

Check the php online manual, for a more extense explanation on echo and print. here:
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Do you indent when you use braces? I don't have much problem at all with braces because I indent the braces to be inline with the block of code they enclose. There are lots of styles for indentation, some more easy to check than other.

Code:
{
code here
  {
  code inside inner braces
  }
}

Also, when using braces, I type in both braces at the same time, then put the code in between them. By creating both braces at once, your biggest problem with them will be tracing down what code you don't want inside the braces, not where you're missing one.

Lee
 
i also dislike the curly braces and so use alternative syntax. I only wish the alternative structures extended to functions!
Code:
function somename():
  //the guts
endfunction;

see [URL unfurl="true"]http://fr.php.net/manual/en/control-structures.alternative-syntax.php[/URL]

as a note of caution: at least one member of this forum has expressed the considered opinion that the alternative syntax for control structures reduces readability.
 
There are also editors which let you highlight one brace and automatically highlight the matching one. That, in concert with good indentation, will let you track down mismatched braces quickly.

Another thing you can do is to have most of your page in HTML and just put <$ $> tags around small "chunks" of php. By breaking up your code this way (or moving some functions to include files), it will also be easier to find mismatched braces.
 
Thanks everyone, when you start on a new language you have to develope your own style.

I like the idea about curly braces, but I didn't know about endfunction.

Readability is everything so I like to have everything properly indented, you just endup confusing yourself.

Richard



 
sorry - the point is that endfunction does not exist. it's the one start->end that does not have the alternative syntax. you must use curly braces for functions. all other control structures you can use the alternative syntax.
 
I would be one of those who has stated that the alternative syntax reduces readability. Because it cannot be used consistently, a person reading your code must be aware of two styles of program blocking, which reduces readability.

The only reason I can see that the alternative syntax was included at all is so that folk coming to PHP from Visual Basic feel more comfortable. You'll find that curly braces are the standard in many other languages (c [and thus C++], perl, Java).



Want the best answers? Ask the best questions! TANSTAAFL!
 
sleipnir214:

you are indeed the voice that i was referring to!

outside of the function and class declarations (which i guess logically are not control structures), where else can the alternative syntax not be used? i haven't consciously come across an example but that could just be an addled mind at work.
 
do...while-loops come immediately to mind. Why would one use a syntax that will work with a while-loop, but not with a do...while-loop?

But curly braces do not designate the extent of control structures. PHP's definition for the use of curly braces is in the manual (here):
Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well.
This means that the use of curly braces are the same in control structures, function definitions, class definitions and try...catch exception handling constructs. I can't say whether their use is the same for variable variables and associative array references inside interpolated string literals (the '"test {$foo['bar']} test"' construct) without digging into the PHP source code, but I suspect these are just two more examples of the same thing.

I have found no reference on the internet that tells my why a programming exception (the alternative syntax) to a perfectly usable statement-blocking construct would have been included in the core of a language, but I know from having built compilers that this exception must increase compiler complexity. And the additional complexity comes without adding any earth-shattering features to the language.



Want the best answers? Ask the best questions! TANSTAAFL!
 
thanks. i hadn't considered do...while and I accept the exception. I suspect it is the only control structure (in non-OOP) that does not have the alternative syntax available. i also accept that as PHP moves further into OOP that the advantages of the alternative syntax will fade away against the advantage of having a single syntax for all control structures.

I also agree that curly braces have uses far broader than control structures but this, to some extent, might be considered to make the alternative syntax more useful from a readability perspective.

no earth shattering features are granted by the alternative syntax, I agree. and i accept that having to parse for both might well impose a processor overhead. but it does provide an easy path to php from both basic languages that use the begin...end structure and the C family. in any event, i guess/hope the processor overhead is likely to pale into insignificance when compared to inefficient coding and (more so) the latency of the web!

 
I have been trying to learn PHP now for three weeks (full time) and to be honest now that I am starting to understand the curly brackets you just "do it"

I had to find a way to indent them as my code was looking a mess.

The hardest thing for me to remember is to put the ";" at the end of each statement otherwise I have to just work at it until I can make the prog operate as I want.

Thanks for all the help, no doubt there will be more "daft" questions.

Richard
 
If you're just starting, please do the PHP community at large a favor and do not learn "alternative syntax"

I fully support sleipnir's assertion that it was added predominantly for the sake of VB developers.

If you're code ever enters the realm of distributable (as in for others to use the code, not just deploy the solution), or you ever work on a group project you're going to be doing yourself a disservice. Any reasonably complex project defines coding standards up front (variable naming, function naming, etc.) and I have yet to hear of a single one which had such foresight and chose the alternative syntax, the only time I see it is when foresight was lacking.

If you were writing a PHP project for me, and I saw the syntax you'd be rewriting it, and not on my dime, because you wouldn't be adhering to the original design docs.

As an aside, any competent text editor should help you with indenting and brace matching.
 
After spending 15 years learning foxpro (and I still don't know all of it), I think it will be many years before I am good enough to distribute code.

I will take on board your comments about not learning alternative syntax (its a struggle to lean the basic one without making it more complex)

I am using macromedia at the mo as a text editor, (it saves to the website as well as the local disk). And if I need a HTML page I can switch it over to design mode & create it in seconds.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top