...

View Full Version : Reading PHP Syntax



username85
10-24-2012, 01:49 AM
Hey all,

Does anybody know/have a book/online source/tutorial(free or otherwise) that teaches you how to read php? I mean the way you would literally say the code in english if you were to read it line by line.


-R

tangoforce
10-24-2012, 01:58 AM
Code doesn't really have an english equivalent. It's all about variables really, comparing, setting, modifying etc.

You're going to have to actually LEARN php if you want to understand it - the same applies to any language. You'll have head scratching moments but thats when you ask for help.

I'm afraid there is no easy way to get the knowledge, you'll just have to learn and experiment with bits of code to see what they do etc.

To help you along the way..
tizag.com has good tutorials
php.net has an excellent function reference manual with user contributed notes (very handy)
google.com usually turns up bits of code you can use, study etc.

username85
10-24-2012, 03:20 AM
With all due respect, I have to strongly disagree with you. Take the following code for example:


if(isset($var)) {
print "This";
} else {
print "That";
}

it can be worded as: if the variable '$var' is set, or exists, print the statement "This", otherwise if it doesn't exists, print the statement "That". Also, if I saw the following function: float round ( float value [, int precision])
I would read it as: the function named 'round' returns a float value, with its first parameter named 'value' being a float and its second(and optional) parameter named 'precision' being a interger.

This is the approach I'm trying to get at, being able to say what the code does. Feedback? Suggestions?


-R

tracknut
10-24-2012, 04:03 AM
I'm curious as to your need for this. I've never heard of this question before, and am honestly wondering what you're going to accomplish with it?

Dave

john6
10-24-2012, 04:24 AM
I'm fairly sure most beginner guides to php would explain it like that.

tangoforce
10-24-2012, 12:14 PM
With all due respect, I have to strongly disagree with you. Take the following code for example:


if(isset($var)) {
print "This";
} else {
print "That";
}

it can be worded as: if the variable '$var' is set, or exists, print the statement "This", otherwise if it doesn't exists, print the statement "That". Also, if I saw the following function: float round ( float value [, int precision])
I would read it as: the function named 'round' returns a float value, with its first parameter named 'value' being a float and its second(and optional) parameter named 'precision' being a interger.

This is the approach I'm trying to get at, being able to say what the code does. Feedback? Suggestions?


-R

Fair enough.. but then why are you asking this question?

BTW your description of round is also wrong - it should be: round takes a parameter that is a float with an optional second paraameter. It returns a value that is an integer. The way you've done it starts at the beginning, goes to the end and then comes back to the middle to explain the parameters.

kbluhm
10-24-2012, 02:47 PM
With all due respect, I have to strongly disagree with you. Take the following code for example:


if(isset($var)) {
print "This";
} else {
print "That";
}

it can be worded as: if the variable '$var' is set, or exists, print the statement "This", otherwise if it doesn't exists, print the statement "That". Also, if I saw the following function: float round ( float value [, int precision])
I would read it as: the function named 'round' returns a float value, with its first parameter named 'value' being a float and its second(and optional) parameter named 'precision' being a interger.

This is the approach I'm trying to get at, being able to say what the code does. Feedback? Suggestions?


-R

It appears you're handling it pretty well. Are you sure you need a book?

Fou-Lu
10-24-2012, 05:26 PM
This is the wrong way around. You don't extract the algorithm (english or not) from the language, you manufacture the code to match the algorithm based on the language. Whilst you can manufacture the algorithm from the language, its much easier to go the other way around.
Example in case:


set result as string value empty
if var is 0 then
set result as "nilslot"
if var is 1 or 2 then
set result as "clause1"
else if var is 3 then
set result as "clause2"
else if (as char)var is '4' or 'v' then
set result as "clause4"
else if (as char)var is 't' then
set result as "tslot"
else
set result as "other"


Algorithmically that is fairly straight forward to read. But there is no need to use an if/else:


switch (k)
{
case 0:
result = "nilslot";
break;
case 1:
case 2:
result = "clause1";
break;
case 3:
result = "clause2";
break;
case '4':
case 'v':
result = "clause4";
break;
case 't':
result = "tslot";
break;
default:
result = "other"
}

// or in another language
(case k ((1 2) 'clause1)
(3 'clause2)
(nil 'no-keys-so-never-seen)
((nil) 'nilslot)
((:four #\v) 'clause4)
((t) 'tslot)
(otherwise 'other)))


So as you can see writing a block of code from an algorithm is far easier done than extracting the algorithm from the code. I adapted the above using examples of the switch statement from here: http://c2.com/cgi/wiki?SwitchStatement; I don't use lisp (one day I'll learn it), but I was aware of the unusual structure of it compared to c based languages, so I thought it would be a good idea to get a switch example of the lisp.

As mentioned though, you seem to read them fine. I doubt that a book would exist for interpreting a language to an algorithm, rather you will likely find books that describe how to write algorithms.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum