Hi everyone, So, I'm new in PHP. I know that PHP is basically inline codes. However, from what I've seen, (and from experiences with ASP classic), sometimes coders would rather write all the functions before any of the html elements. And in the inline codes, they'll call the functions/variables. e.g. <?php $var = 'test' .... codes codes functions functions... ?> <div> <?php echo $var ?> </div> or.. <div> <?php echo 'test' ?> </div> which one is the better style?
Ideally you should completely (or as much as possible) separate logic from the html/output portion of the coding. This provides much better organization. It may not necessarily be as efficient, but efficiency comes in at a distant second on a large, complex, project where organization is absolutely essential. MVC is a popular method of segmenting different parts of code.
Just to build on what jestep posted, you'll almost always have to nest PHP code with HTML code (unless you use a template engine like Smarty which, for over 99% of projects I would personally argue against). However, like jestep stated, good practice involves separating logic as much as you can from your output). For example, code that queries a database for data, and transforms that data in to some meaningful form of information should ideally be in a separate file to your HTML code. It's generally good practice for your HTML file to take that information, iterate through and present it to the user. You can imagine how hard it would be for yourself, let alone other programmers who come on to a project, to sift through a file thousands of lines long with PHP and HTML code randomly all over the place.
yea, that makes sense! thanks! the first language i learned was asp classic. and the codes i looked at were in between the html tags. i found it easy to understand but a little too messy - though it did become very confusing when it comes to large junk of codes. thanks a lot!
I also recommend MVC, however mixing everything is possible. It's just a lot of hassle, and difficult to see through as well as understand. In the start I wrote php mixed with html, cause I didn't know better though. If you like to avoid MVC for any reason, then It's highly recommended that you keep your functions at the top. This will help you get a rid of many header errors..
I would say none! If you would display data you can use <?=$var;?> And if you are using variables in stings use " otherwise use ' so $text = "hello $world"; or $text = 'Hello '. $world; And if you want to do more with MVC please start just by learning the language proper! the idea behind MVC is to split code and design but for a beginner, don't even start minding that... just make your scripts and enjoy them!
If you're looking for something to organize your work, then you can try NetBeans IDE, a software developed by Oracle which helps you organize your code.
You should not be using short tags anymore. And I disagree. A firm understanding of the theory behind programming (as in the philosophy behind MVC), even for a beginner will teach you to write clean, efficient code. Why even bother wasting time programming poorly and learning bad habits?
Learn MVC mate. Once you get the hang of it, it'll greatly reduce the time you take to build sites. Especially when your database is wrapped in an ORM. To answer your question though, your functions and logic should go at the top before the <html> tag. This allows you to do error checking and redirects before any headers are sent. (you cant redirect after output is started to the browser).