makuc Posted July 4, 2011 Share Posted July 4, 2011 Hi. My name is Armin and what can I say? I am new to php and I am self teaching ... Yea, you get what I mean. So I have some basic noob questions. First off: I have been playing around with my code a little and suddenly find out that I have to ask something that I am not that sure about. So the thing is (english is not my primary language, so if anyone would need any more explanation about what I mean, please ask me to!) ... so, does PHP code gets calculated line by line? Because I have been playing around with registering script and I came out that if I place header redirection at the beginning of the code none of the code under it gets processed (meaning that form elements doesn't get send to the MySQL database). So I haven't tried about putting it at the end yet, because as I said, I want to be sure about that for future coding . Once this BIG question is resolved, I'll post another one . Thanks in advance. PS. If anyone want to see what I mean, feel free to ask . Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 4, 2011 Share Posted July 4, 2011 Yes line by line from top to bottom. header redirection before any output to browser, and will also go somewhere, code below will not execute. Quote Link to comment Share on other sites More sharing options...
makuc Posted July 4, 2011 Author Share Posted July 4, 2011 Thanks for your fast reply ... I am glad that my thinking was right. Well, too bad that that's it for today...I have to get some sleep now. Well, more questions coming tomorrow. Thanks again. I'll sleep a way better now . Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 4, 2011 Share Posted July 4, 2011 Post coding problems here. general questions should go here. http://www.phpfreaks.com/forums/index.php?board=14.0 Quote Link to comment Share on other sites More sharing options...
makuc Posted July 5, 2011 Author Share Posted July 5, 2011 Before I started with my registration script, I have been trying with real-time server clock. I know that PHP gets calculated once, only, and after that nothing happens with it until the page refresh. I have heard that something like real-time clock could be done using AJAX, but I also heard that if a lot of users would be visiting a webpage, something like that would make server overloading, after some time. So here is my question ... Is there really no other way to do it? Because I have been thinking: JavaScript is calculated in local (user) PC and yes, I know, that all that JS can get is some local data, but if someone make JS keep tracking seconds only and even those just when they change, nothing more, it could change the time of server time, got by PHP. Something like that: PHP get some data from server like <?php $serverTime = date(...);?> After that, JS comes in: <script language=javascript><!-- function RealTimeClock() { var serverTime=<?php echo($serverTime);?> .....some code i don't know about, but as I said, it would track local time seconds (only) changes (no matter if they rise or lower) and whenever a change is done, it rises server time SECONDS for 1. After that with some more JS code it would auto calculate once seconds are higher that 60, it auto rise minutes for 1 and so on ..... } --></script> As I said, I have been studying PHP for a little (I am still a noob ), but I have not even a clue about JS, so any help would be appretiated. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Adam Posted July 5, 2011 Share Posted July 5, 2011 Your logic about passing the time stamp into the JavaScript is correct, and widely used. I don't follow this though: whenever a change is done, it rises server time SECONDS for 1 Why would you need to ping back the server with the new time, or what ever you hand in mind? You got the time from the server, it's been x seconds since the last request, the server time will have advanced x seconds also. Maybe I don't understand what you're actually trying to achieve..? Quote Link to comment Share on other sites More sharing options...
makuc Posted July 5, 2011 Author Share Posted July 5, 2011 Why would you need to ping back the server with the new time, or what ever you hand in mind? You got the time from the server, it's been x seconds since the last request, the server time will have advanced x seconds also. Maybe I don't understand what you're actually trying to achieve..? Actually I didn't mean it like that ... Since PHP can't make realtime showing clock from server time (all it does is collect the infos at the time when it is executed), I would want that JS detect whenever there is a change in seconds on the user machine (the value of change doesn't matter, just that there is a change) and it would update (perhaps I just tell it wrong) the time that PHP gives at the beginning. Perhaps there should be some more different values (more separated, I mean) give from PHP like seconds by themselves, minutes, hours etc. And then they all get merged in JS. It's just that let's say I manage to get my site pretty known with quiet some traffic (let's hope so) and the fact that for now I am on sharing server ... you know what I mean, so I don't really like to make too overloading for nothing to the server itself using AJAX, to rapidly execute the part of code. So yea, that is basicly what I am up to . It's just that user can change his clock and I really don't want that to affect the clock on my site . Quote Link to comment Share on other sites More sharing options...
ignace Posted July 5, 2011 Share Posted July 5, 2011 JS is a fun language to play around in. For example if you are running Chrome or IE8/9 press F12. A new dialog should appear on the bottom of your screen, press Console. At the bottom you'll find an input field. Type console.dir(new Date()); You'll find a __proto__ property in Chrome, expand it to find the available methods. Now type: console.dir(console); And admire Output may differ between vendors. Chrome for example shows memory and profiles properties along with the known __proto__ property. Whenever I wonder how something works or will it work? I use this method to find it out quickly while I'm coding. You can run PHP from the command line (on Windows [Windows Key]+R), so that looks like: php -r "var_dump([whatever neat stuff you want to check comes here]);" Quote Link to comment Share on other sites More sharing options...
Adam Posted July 5, 2011 Share Posted July 5, 2011 When you construct the Date object you pass in the time stamp from PHP, the server time, so that it's set to the exact same time as the server. Then if you increment the object's time every second it would always be equal to the server. Working clock's are very.. Gimmicky though. There's a reason why you don't see them very often. Quote Link to comment Share on other sites More sharing options...
makuc Posted July 5, 2011 Author Share Posted July 5, 2011 I know, that's why I am trying to make one . And I would really appretiate any help with that . *********************************************************************************************************** But for today I have enough studying . See you all tomorrow. And if anyone could write JS for something like that, I would be really greatfull. Otherwise, I think I will revive this project tomorrow and keep you updated. PS. Thanks for your advice, Ignace. I'll keep that in mind, tommorow. xD Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 6, 2011 Share Posted July 6, 2011 Yes line by line from top to bottom. That's not entirely true. The following outputs hello: <?php test(); function test() { echo 'hello'; } If it was strictly line by line, top to bottom, you couldn't execute this because the test() function didn't exist yet. To understand why this is the case, you need to understand how interpreters/compilers work. The work is typically split into multiple, distinct steps. The first step is called lexical analysis, which is the process of turning a series of characters into a series of "tokens". For instance, the string echo in PHP corresponds to the T_ECHO token in PHP (the list of tokens in PHP can be found at http://php.net/tokens). The part of the program that does this is typically called a lexer or a tokenizer. The string "echo 1+1" would result in the following tokens: T_ECHO, T_WHITESPACE, T_LNUMBER, +, T_LNUMBER. The next step is syntax analysis (also called parsing), which takes a series of tokens as input and gives an "abstract syntax tree" (AST) as output. It does this using a predefined grammar that consists of a set of "productions" consisting of "terminals" and "non-terminals". Typically you would describe this grammar using a formalized notation such as (Extended) Backus-Naur Form ((E)BNF) and use a parser generator to get a parser, because hand-coding it is tedious and error prone. The above example string would result in the following tree: T_ECHO | + / \ T_LNUMBER(1) T_NUMBER(2) A simple EBNF grammar for a mathematical expression (integers only) with the common arithmetic operators and grouping with parentheses could look like this (precedence rules work from top to bottom): digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; number = digit, { digit } ; expr = number | plus | minus | div | mult | group ; group = "(", expr, ")" ; mult = expr, "*", expr ; div = expr, "/", expr ; plus = expr, "+", expr ; minus = expr, "-", expr ; Here, each line is a production, each thing in quotes is called a terminal and the others are called non-terminals. Given a set of productions, you can using a process called derivation. There exist multiple strategies for doing this, e.g. LL(1) parsing, SLR parsing, etc. After the parsing, you can have a type checker, e.g. to ensure that both operands of an arithmetic operator are guaranteed to be numeric. PHP does runtime type inference, so it doesn't have a type checker. There is no difference between a compiler and an interpreter up until this point. At this point, an interpreter will evaluate the AST by recursively traversing it. A compiler will transform the AST into a another, typically lower level, language. Again, this would be using recursive traversal. This may be something like LLVM, bytecode for JVM, assembler for some CPU architecture or something else. In theory, there is nothing preventing you from taking a PHP AST and turning it into Python source code, or English prose describing what is going on if it would be executed. When evaluating the AST, you may create a lookup table consisting of in-scope variables, functions, etc. header redirection before any output to browser, and will also go somewhere, code below will not execute. Again, this is not entirely true. To understand why this is the case, you need to understand how the HTTP protocol works. HTTP is a stateless protocol defined in RFC 2616, which works by sending a request (with some headers) and getting a response in return (with some headers). To login, my user agent (e.g. a browser) could send a request like this: POST /login.php HTTP/1.1 Host: www.example.com User-Agent: whatever Content-Type: application/x-www-form-urlencoded ***possibly more headers*** username=Daniel&password=s3cret When the web server receives this request, it may "forward" it to PHP so it can generate an appropriate response. Assuming the credentials given are correct, you may choose to set some cookie so you can remember him (remember, HTTP is stateless and all requests are entirely separated) and you may use the Location response header to indicate to the client that it should go somewhere else when it is done. However, this doesn't stop the execution of your script. Doing this could be problematic as well. You might still have some cleaning up to do (closing connections to other places, releasing file locks, etc.), or you may have more headers you want to send. If you want the execution to stop, you'll have to explicitly do it using die()/exit() or simply make sure that there are no executable statements after. When you have done this, the web server may send something like the following request back over the same TCP socket: HTTP/1.1 200 OK Location: http://www.example.com/index.php Set-Cookie: session_id=kasdfusd89f79ads8f7g Actually, RFC 2616 says that a user agent "MUST NOT" automatically redirect as a result of a Location response header without user intervention if the request was not GET or HEAD, but most browsers seem to ignore that. At any rate, the Location is only a suggestion for the client to retrieve the new resource. The client is free to ignore it. You may verify that this is true by placing the following file in a web-accessible directory and requesting it using a browser: <?php header('Location: http://www.google.com/'); file_put_contents('new_file.txt', 'Was not here before...'); ?> Assuming the web server has write permissions in that directory, you should find a new file called new_file.txt in that directory even though your browser was redirected to Google. Quote Link to comment Share on other sites More sharing options...
makuc Posted July 6, 2011 Author Share Posted July 6, 2011 Actually, RFC 2616 says that a user agent "MUST NOT" automatically redirect as a result of a Location response header without user intervention if the request was not GET or HEAD, but most browsers seem to ignore that. At any rate, the Location is only a suggestion for the client to retrieve the new resource. The client is free to ignore it. You may verify that this is true by placing the following file in a web-accessible directory and requesting it using a browser: <?php header('Location: http://www.google.com/'); file_put_contents('new_file.txt', 'Was not here before...'); ?> Assuming the web server has write permissions in that directory, you should find a new file called new_file.txt in that directory even though your browser was redirected to Google. Hmmm, interesting. When I have been playing around with register script, written in php, and I put the header on the top, it just didn't do anything in the database (MySQL). That's why I ask here. Of course there was another error in the code somewhere, but as fast as I remove the header from the top, it at least create a database (and I am 99.9% sure that I DID NOT create it!). So right now I am kind of confused. Sorry, I haven't read your post yet, but I will. I just flow over it a little and find out these . So thanks for your reply. I'll check it later on for a complete analyze . Quote Link to comment Share on other sites More sharing options...
makuc Posted July 6, 2011 Author Share Posted July 6, 2011 This is what I have so far : <?php $sec = date(U); $seconds = date(s); $minutes = date(i); $hours = date(H); $day = date(d); $dayweek = date(w); $months = date(m); $year = date(Y); ?> <script type="text/javascript"> var s=<?php echo($seconds);?>; var t; var secCounter_is_on=0; function seconds() { document.getElementById('Clock').innerHTML="Current seconds are: " + s; s=s+1; t=setTimeout("seconds()",1000); } function secStart() { if (!secCounter_is_on) { secCounter_is_on=1; seconds(); } } </script> <body onLoad="secStart()"> <p id="Clock">Server seconds should be shown here!</p> Please, tell me what you think, and please post any ideas how should I make it auto increase minutes once seconds are <= 60. PS.: You can preview it at: http://cybital.cu.cc/Clock.php Quote Link to comment Share on other sites More sharing options...
makuc Posted July 6, 2011 Author Share Posted July 6, 2011 And could anyone take a look at this clock and try to modify it so it will increase the time and not decrease? http://x10gaming.com/ I guess that I wouldn't have to one myself and with help of all of you, of course PS.: @Daniel0 from your post, I can see that I still have a long way to go . But I must admit, your post is really helpful for my basic PHP understanding. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 6, 2011 Share Posted July 6, 2011 Daniel0 from your post, I can see that I still have a long way to go . But it is really helpful for my basic PHP understanding. You don't need to understand the first part of my post to become a PHP programmer. I only touched the tip of the iceberg. If you find it interesting and you'd like to learn about compiler theory, I would recommend you to read a book or take a course on compilers (the latter would likely include the former as well though). I'm a computer science student, so compiler and formal language theory are some of the subjects I deal with, but you don't need to become a computer scientist to become a web developer. I don't expect you to understand all of it at this point, but there should be enough keywords for you to research should you decide to look at it further. An understanding of the HTTP protocol is pretty essential if you want to become a web developer though, even if you at first only have a rudimentary understanding of it. Quote Link to comment Share on other sites More sharing options...
makuc Posted July 6, 2011 Author Share Posted July 6, 2011 Daniel0 from your post, I can see that I still have a long way to go . But it is really helpful for my basic PHP understanding. You don't need to understand the first part of my post to become a PHP programmer. I only touched the tip of the iceberg. If you find it interesting and you'd like to learn about compiler theory, I would recommend you to read a book or take a course on compilers (the latter would likely include the former as well though). I'm a computer science student, so compiler and formal language theory are some of the subjects I deal with, but you don't need to become a computer scientist to become a web developer. I don't expect you to understand all of it at this point, but there should be enough keywords for you to research should you decide to look at it further. An understanding of the HTTP protocol is pretty essential if you want to become a web developer though, even if you at first only have a rudimentary understanding of it. Hmmm, interesting, I am actually getting interested in reading PHP and MySQL Bible again after all (i have started, but after that decided that 1400 pages is a way too much ). What do you think of my clock? I have actually just implemented in minutes so hours are coming soon, too . You (and anyone else interested in it) can check it on: http://cybital.cu.cc/clock.php Quote Link to comment Share on other sites More sharing options...
makuc Posted July 6, 2011 Author Share Posted July 6, 2011 Is there any way that I could start function from JavaScript without using body onLoad ? Because I really want to make the whole thing simpler to integrate to anyones website, so he wouldn't have to much trouble if he would already have any body onLoad function set already :-\ . So all he would do is set an id="clock" where he want the clock to appear . Thanks in advance. PS. How can I edit any of my earlier posts? Because after some minutes, the button edit just disappear. Quote Link to comment Share on other sites More sharing options...
Philip Posted July 6, 2011 Share Posted July 6, 2011 <div id="clock"></div> <script type="text/javascript"> myFunc(); </script> Quote Link to comment Share on other sites More sharing options...
makuc Posted July 6, 2011 Author Share Posted July 6, 2011 <div id="clock"></div> <script type="text/javascript"> myFunc(); </script> Thanks. I didn't know, that JavaSript will get executed itself only if it is placed under the first line written in HTML (which is automatically body tag), so thank you very much. And my Server Clock is finally finished! It is completely independent from the user clock which I really didn't expect . But yes, here it is, real-time server clock. The latest link to it is already in my previous post and since I don't know how long will it be there (I'll try to keep it that way as long as I can ), otherwise, if anyone need it, please pm me. BTW, here is link to it one more time: http://cybital.cu.cc/clock.php Quote Link to comment Share on other sites More sharing options...
makuc Posted July 7, 2011 Author Share Posted July 7, 2011 @Daniel0 Do you know for any good book of PHP and JavaScript that are good for start? Because for now I have been studying from cheat sheets only (since I left PHP and MySQL Bible ) and of course uncle Google . http://w3school.com/ is pretty good for a start at least from my opinion. This discussion was opened for helping to start with programming for noobs, like I am, so if anyone know for any good resources for PHP and JavaScript, the reply here is VERY welcome . And thanks for all your replies and help until now. With your help I was able to finish my first project using PHP and JS (and let's not mention that I have already give up with it once ), so keep it up . Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted July 7, 2011 Share Posted July 7, 2011 w3schools is bad. See the link in my signature for more info. For PHP, nothing beats the online manual: http://php.net/manual/en/langref.php And the quick function reference: http://www.php.net/quickref.php For HTML, CSS, and JavaScript, use the Mozilla Developer Network: https://developer.mozilla.org/en-US/learn/ Quote Link to comment Share on other sites More sharing options...
trq Posted July 8, 2011 Share Posted July 8, 2011 Is there any way that I could start function from JavaScript without using body onLoad ? Because I really want to make the whole thing simpler to integrate to anyones website, so he wouldn't have to much trouble if he would already have any body onLoad function set already :-\ . So all he would do is set an id="clock" where he want the clock to appear . Thanks in advance. PS. How can I edit any of my earlier posts? Because after some minutes, the button edit just disappear. This code creates a self executing function. <script type="text/javascript"> (function() { // your code here })(); </script> Quote Link to comment Share on other sites More sharing options...
makuc Posted July 8, 2011 Author Share Posted July 8, 2011 w3schools is bad. See the link in my signature for more info. Well, thank god that I have just read there some infos about specified peaces of code from different cheat sheets , nothing more But thank you for warning, since I was just about to start reading a lot more there . And thank you for all links. They will come in handy, you can be sure about that . Thorpe, thanks for that tip ... I'll test it soon, after I make some more upgrades to the clock (so it will specify how many days a month have, before it break and start counting in a new, than when February have 28 and when 29 days, etc.). So yea, thanks for the tip. Quote Link to comment Share on other sites More sharing options...
makuc Posted July 8, 2011 Author Share Posted July 8, 2011 Does anyone know, how could I make a redirector, or something, that would look if in the same directory exist a file with the same name, but is let say spelled with capital letters or only some of them ... The problem is that in my-early stages of my Real-Time PHP & JavaScript Server Clock, I have been previewing it with a file name Clock.php (capital c), but since I didn't want it to be affected by my spelling errors in kode (yea, it wasn't working all the time ... ) i change the name for testing purposes. But then, when I think that it can be published, I changed the name back to clock.php, but forget to make first c capital and since I wanted to put the link there one more time, I copy and paste it again ... and I really don't want to have multiple files on server . Thanks. Quote Link to comment Share on other sites More sharing options...
makuc Posted July 10, 2011 Author Share Posted July 10, 2011 <script type="text/javascript"> <!-- function numDaysPerMonth(){ if(month==1){ if(day==32){ day=1; month=month+1 } } if(month==2){ if(nextLeapYear<year){ nextLeapYear=nextLeapYear+4 } if(nextLeapYear==year){ if(day==30){ day=1; month=month+1; } } else(day==29){ day=1; month=month+1 } } if(month==3){ if(day==32){ day=1; month=month+1 } } if(month==4){ if(day==31){ day=1; month=month+1 } } if(month==5){ if(day==32){ day=1; month=month+1 } } if(month==6){ if(day==31){ day=1; month=month+1 } } if(month==7){ if(day==32){ day=1; month=month+1 } } if(month=={ if(day==32){ day=1; month=month+1 } } if(month==9){ if(day==31){ day=1; month=month+1 } } if(month==10){ if(day==32){ day=1; month=month+1 } } if(month==11){ if(day==31){ day=1; month=month+1 } } if(month==12){ if(day==32){ day=1; month=month+1 } } } --></script> Can anyone help me? I can't figure out what is wrong in the code. But I know where the error comes in, it is in this part of the code: if(month==2){ if(nextLeapYear<year){ nextLeapYear=nextLeapYear+4 } if(nextLeapYear==year){ if(day==30){ day=1; month=month+1; } } else(day==29){ day=1; month=month+1 } } So, if I remove the if(...){...} and change else to if, it works fantastic. I really want to get this to work. Thanks. PS.: This is just one part of my clock code. If you need to see it whole, feel free to ask. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.