Little help with my php code pls
-
I always include the form processing at the top to the same page and allow the logic to either load a successful registration page on success or reload the same page with errors marked in the form. The general form is
I often leave the action empty as it will default to the same page.
Hope this helps
Why do I need to know this?
cjoki wrote:
$pg = "\n"; $pg.= "\n"; $pg.= " Register\n"; $pg.= "\n"; $pg.= "\n"; $pg.= "
\n"; $pg.= "Name: :omg: :wtf: Why are you assigning it to a variable? It's quicker just to output it directly onto the screen.
Register
Name:
Also use ' to encapsulate strings as they are quicker than ", as " marks compile the string to check for variables, hence why they are slower. I suggest you look into using a template engine, such as Smarty[^], to format your pages.
If at first you don't succeed, you're not Chuck Norris.
-
Why do I need to know this?
cjoki wrote:
$pg = "\n"; $pg.= "\n"; $pg.= " Register\n"; $pg.= "\n"; $pg.= "\n"; $pg.= "
\n"; $pg.= "Name: :omg: :wtf: Why are you assigning it to a variable? It's quicker just to output it directly onto the screen.
Register
Name:
Also use ' to encapsulate strings as they are quicker than ", as " marks compile the string to check for variables, hence why they are slower. I suggest you look into using a template engine, such as Smarty[^], to format your pages.
If at first you don't succeed, you're not Chuck Norris.
I have used this style for some time and it works fine and gives me better control over the output. With echo once something is echoed its gone to the browser. Any performance hits from the quote style has never been an issue. Also its just habit, I generally write much bigger forms that have variables worked into the HTML output...and why are you asking me to look into a template system when your concern is performance? As for smarty...or any other template engine...why bother? PHP IS my template engine. This style of code allows me to separate PHP and HTML from css and js. That allows me to write large scripts and maintain complete control over the source and keep it clean and easy to read, and thus maintain. Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.
modified on Monday, October 12, 2009 2:08 PM
-
I have used this style for some time and it works fine and gives me better control over the output. With echo once something is echoed its gone to the browser. Any performance hits from the quote style has never been an issue. Also its just habit, I generally write much bigger forms that have variables worked into the HTML output...and why are you asking me to look into a template system when your concern is performance? As for smarty...or any other template engine...why bother? PHP IS my template engine. This style of code allows me to separate PHP and HTML from css and js. That allows me to write large scripts and maintain complete control over the source and keep it clean and easy to read, and thus maintain. Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.
modified on Monday, October 12, 2009 2:08 PM
cjoki wrote:
This style of code allows me to separate PHP and HTML from css and js.
How is
$pg = "\n";
separating the two? You include the HTML IN your PHP. CSS and JS can be separated from HTML into several separate files (using thelink
tags and thescript src
attribute, respectively), rather than being scripted in, so to say. I prefer having separate files. When separating JS and CSS there are always certain bits which have to overlap such ass the class attributes. Smarty does the same for PHP and HTML. This is a very basic example of using Smarty. index.php<?
include('Smarty.class.php');
$Smarty = new Smarty();
$Smarty->assign( 'message' , 'Smarty Rocks!!!' );
$Smarty->display( 'index.tpl' );
?>index.tpl
Smarty Example {$message}
cjoki wrote:
Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.
After looking at the above code, you can't say that it's not insanely simple.
cjoki wrote:
and why are you asking me to look into a template system when your concern is performance?
Quite simply, I don't actually do the website design (I'm colour blind), I just worry about the functionality. The designer I hire designs the site and then writes the HTML and CSS and leaves me to do all the JS and server side, without having to get in my way. So if he wants to redesign the site then he just changes the templates, rather than having to troll through my PHP code, the same could be said if I want to redo the back-end of the site. Have a read of this[^].
If at first you don't succeed, you're not Chuck Norris.
-
cjoki wrote:
This style of code allows me to separate PHP and HTML from css and js.
How is
$pg = "\n";
separating the two? You include the HTML IN your PHP. CSS and JS can be separated from HTML into several separate files (using thelink
tags and thescript src
attribute, respectively), rather than being scripted in, so to say. I prefer having separate files. When separating JS and CSS there are always certain bits which have to overlap such ass the class attributes. Smarty does the same for PHP and HTML. This is a very basic example of using Smarty. index.php<?
include('Smarty.class.php');
$Smarty = new Smarty();
$Smarty->assign( 'message' , 'Smarty Rocks!!!' );
$Smarty->display( 'index.tpl' );
?>index.tpl
Smarty Example {$message}
cjoki wrote:
Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.
After looking at the above code, you can't say that it's not insanely simple.
cjoki wrote:
and why are you asking me to look into a template system when your concern is performance?
Quite simply, I don't actually do the website design (I'm colour blind), I just worry about the functionality. The designer I hire designs the site and then writes the HTML and CSS and leaves me to do all the JS and server side, without having to get in my way. So if he wants to redesign the site then he just changes the templates, rather than having to troll through my PHP code, the same could be said if I want to redo the back-end of the site. Have a read of this[^].
If at first you don't succeed, you're not Chuck Norris.
I could have stated this better, but I meant php and html, then js, and then css are seperated and controlled by code logic. I do entire projects, including layout and design, programing and db design. I have no need to separate the page further, than is needed by css and js, or even to keep the code more manageable. Also if you know php you can easily maintain my code. There is no need to learn additional language rules required by a template engine. My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment. ...and I counter your simple smarty code with an even simpler php code that only requires 1 file and will run..much...much faster!
<?php
$msg = "Smarty What...?";/* I know you like the echos... ;) */
echo "";
echo "";
echo " PHP Example";
echo "";
echo "";
echo "<h1>".$msg."</h1>";
echo "";
echo "";
?>I sure we can go tit for tat, but this has gone way of topic from the original post. My reply was not meant to start a flame war, but to offer another option to the author.
-
I could have stated this better, but I meant php and html, then js, and then css are seperated and controlled by code logic. I do entire projects, including layout and design, programing and db design. I have no need to separate the page further, than is needed by css and js, or even to keep the code more manageable. Also if you know php you can easily maintain my code. There is no need to learn additional language rules required by a template engine. My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment. ...and I counter your simple smarty code with an even simpler php code that only requires 1 file and will run..much...much faster!
<?php
$msg = "Smarty What...?";/* I know you like the echos... ;) */
echo "";
echo "";
echo " PHP Example";
echo "";
echo "";
echo "<h1>".$msg."</h1>";
echo "";
echo "";
?>I sure we can go tit for tat, but this has gone way of topic from the original post. My reply was not meant to start a flame war, but to offer another option to the author.
I'm sorry, but I only echoed the variable, NOT the rest.
<?
$msg = 'Smarty is cool';
?>PHP Example <h1><? echo $msg; ?></h1> <h1><?=$msg?></h1>
No unnecessary processing wasted by unnecessary echoing, holding all the output in memory or wasted time compiling strings using " instead of '. Which is what I said in the first place. Template engines generate it all for you, quite efficiently. They are only sluggish when compiling a template for the first time. I have to admit that it did take me a very long time (almost 5 years) to convert.
cjoki wrote:
My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment.
Using the template engine doesn't make my file system any more complicated:
/
classes/
logs/
smarty/
templates/
templates_c/ // Where the template is compiled to.
htdocs/
css/
js/
img/Enough of the flame war. I call a truce, apologies.
cjoki wrote:
With echo once something is echoed its gone to the browser.
Have a look at Output Buffering[^]. It's quicker than both of our methods. I was going to talk about it when talking about the header redirection, but it's too advanced for what he is doing. I'm thinking about writing a couple of articles (when I have time) about OOP in PHP. It'll be more appropriate to continue the discussion then.
If at first you don't succeed, you're not Chuck Norris.
-
I'm sorry, but I only echoed the variable, NOT the rest.
<?
$msg = 'Smarty is cool';
?>PHP Example <h1><? echo $msg; ?></h1> <h1><?=$msg?></h1>
No unnecessary processing wasted by unnecessary echoing, holding all the output in memory or wasted time compiling strings using " instead of '. Which is what I said in the first place. Template engines generate it all for you, quite efficiently. They are only sluggish when compiling a template for the first time. I have to admit that it did take me a very long time (almost 5 years) to convert.
cjoki wrote:
My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment.
Using the template engine doesn't make my file system any more complicated:
/
classes/
logs/
smarty/
templates/
templates_c/ // Where the template is compiled to.
htdocs/
css/
js/
img/Enough of the flame war. I call a truce, apologies.
cjoki wrote:
With echo once something is echoed its gone to the browser.
Have a look at Output Buffering[^]. It's quicker than both of our methods. I was going to talk about it when talking about the header redirection, but it's too advanced for what he is doing. I'm thinking about writing a couple of articles (when I have time) about OOP in PHP. It'll be more appropriate to continue the discussion then.
If at first you don't succeed, you're not Chuck Norris.
I never write broken HTML, large projects to easily become a mess to read. And honestly I have never had an issue with performance. But I would be interested in your article when it is ready... I am familiar with Output Buffering, but have not used it all that much, although maybe I should. Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.). I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test. All the best cjoki
modified on Monday, October 12, 2009 5:12 PM
-
I never write broken HTML, large projects to easily become a mess to read. And honestly I have never had an issue with performance. But I would be interested in your article when it is ready... I am familiar with Output Buffering, but have not used it all that much, although maybe I should. Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.). I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test. All the best cjoki
modified on Monday, October 12, 2009 5:12 PM
cjoki wrote:
Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.).
You get a thumbs up :thumbsup: That's the kind of thing I enjoy doing. Have a look at using jQuery. jQuery could do what you want with a single line.
$(document).ready(function() {
$('#btn').click(some_function);
});(Added new lines to make it more readable.)
cjoki wrote:
I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test.
I was playing around with this the other day. For performance, minimizing your js files, using a tool like this[^], greatly improves page load times. The same applies for css file. Loading multiple small css files is slower than loading one big css file. There is a css compressor[^] as well. Note: It's only worth minimizing files on release versions. A good tool for testing page load time is the Firebug addon[^] for Firefox. It has a tab called 'NET' which shows you how long everything takes to load. It also debugs JS which is very useful and shows the DOM while JS is changing it. Hope that helps :)
If at first you don't succeed, you're not Chuck Norris.
-
cjoki wrote:
Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.).
You get a thumbs up :thumbsup: That's the kind of thing I enjoy doing. Have a look at using jQuery. jQuery could do what you want with a single line.
$(document).ready(function() {
$('#btn').click(some_function);
});(Added new lines to make it more readable.)
cjoki wrote:
I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test.
I was playing around with this the other day. For performance, minimizing your js files, using a tool like this[^], greatly improves page load times. The same applies for css file. Loading multiple small css files is slower than loading one big css file. There is a css compressor[^] as well. Note: It's only worth minimizing files on release versions. A good tool for testing page load time is the Firebug addon[^] for Firefox. It has a tab called 'NET' which shows you how long everything takes to load. It also debugs JS which is very useful and shows the DOM while JS is changing it. Hope that helps :)
If at first you don't succeed, you're not Chuck Norris.
yeah, I keep forgetting about firebug's Net tab for download times, I tend to use it when debugging ajax calls. I also found a tool from yahoo, called Y!Slow that is a firebug plug-in. I have yet to play with it much but it also shows performance slow downs and offers corrective tips. On the css, my question comes down to 1) multiple css files, 2) one big css file or 3) adding css rules to a style tag in the page by way of code logic. Your reply confirms my gut feeling about a large number of small css files (is this true if the files are only added to the servers output on the fly and as needed?), but then there is the 1 large file vs the dynamic css rules in a style tag. With 1 large file there is no control over the css output...that I can think of. But the flow goes; page is requested by browser and then the css is request after the page download completes. The code added to the pages style tag, can use some simple logic rules to minimize the amount of css rules added to the page, thus making it small (vs the entire css file) and would not require a secondary file request for a separate css file (the 1 large css file). It would seam logical that the dynamic css rules added to the page will be the better performance solution. Of course I am not including compressed css files which is another performance question. JQuery, I have avoided...no real reason, but I have. I am still hand coding JavaScript.
-
yeah, I keep forgetting about firebug's Net tab for download times, I tend to use it when debugging ajax calls. I also found a tool from yahoo, called Y!Slow that is a firebug plug-in. I have yet to play with it much but it also shows performance slow downs and offers corrective tips. On the css, my question comes down to 1) multiple css files, 2) one big css file or 3) adding css rules to a style tag in the page by way of code logic. Your reply confirms my gut feeling about a large number of small css files (is this true if the files are only added to the servers output on the fly and as needed?), but then there is the 1 large file vs the dynamic css rules in a style tag. With 1 large file there is no control over the css output...that I can think of. But the flow goes; page is requested by browser and then the css is request after the page download completes. The code added to the pages style tag, can use some simple logic rules to minimize the amount of css rules added to the page, thus making it small (vs the entire css file) and would not require a secondary file request for a separate css file (the 1 large css file). It would seam logical that the dynamic css rules added to the page will be the better performance solution. Of course I am not including compressed css files which is another performance question. JQuery, I have avoided...no real reason, but I have. I am still hand coding JavaScript.
The method I've used recently is to use one big css file. The browser normally caches it, so the only real performance loss will be the first page load. I went through a phase of having a separate specific css file for every page, and a global one included on each page. It was annoying, to say the least, worse to maintain than templates. Plus each page had to cache a separate page which slowed the load time. I tend to cache as many things as I can as it produces quicker load times and doesn't take up as much precious bandwidth :)
If at first you don't succeed, you're not Chuck Norris.
-
The method I've used recently is to use one big css file. The browser normally caches it, so the only real performance loss will be the first page load. I went through a phase of having a separate specific css file for every page, and a global one included on each page. It was annoying, to say the least, worse to maintain than templates. Plus each page had to cache a separate page which slowed the load time. I tend to cache as many things as I can as it produces quicker load times and doesn't take up as much precious bandwidth :)
If at first you don't succeed, you're not Chuck Norris.