
johnmerlino1
Members-
Posts
21 -
Joined
-
Last visited
Everything posted by johnmerlino1
-
I came across the PHP clone method. Does this clone first level fields and methods or does it also do deep copying e.g. copying references to those fields. For example, a field may be an array. Does it make a copy of the actual array or does it refer to the same array position in memory, so if the original changes, then the copy's changes as well?
-
closing php tag in function to write html good practice?
johnmerlino1 replied to johnmerlino1's topic in PHP Coding Help
-
thanks for thoughtful response.
-
I'm reading over this book and I came across something that looked very odd: public function DisplayHeader() { ?> <table width="100%" cellpadding="12" cellspacing="0" border="0"> <tr bgcolor ="black"> <td align ="left"><img src = "logo.gif" /></td> <td> <h1>TLA Consulting Pty Ltd</h1> </td> <td align ="right"><img src = "logo.gif" /></td> </tr> </table> <?php } In the function it closes a php tag, uses raw html that is rendered as is on the page, and then returns back to opening the php tag. I understand why it is being done. It is easier to write raw html here than to echo it in a php block, but it just looks odd. Anyone actually use this technique?
-
Do interfaces in PHP include default methods, which are available in Java 7 and 8. Default methods allow you to add default functionality in the interface itself. So when a class implements it, it can use the default definition from the interface. This is in contrast to an abstract method which has no method body.
-
First, I read PHP does not support function overloading. Then later in the book in chapter of OOP, it states: So does it support function overloading or not? And of course my definition of function overloading is what you get in the Java programming language.
-
So basically unset is the equivalent of setting an initialized value in Java back to null.
-
In C, when you dynamically allocate memory, you have to free it, otherwise you face memory leaks: char *str; str = (char *) malloc(10); free(str); But in most other languages you do not need to worry about this. PHP has a function called unset, which unsets the variable's value. But why would you want to do this, if PHP has a garbage collector that will free variables once they leave scope anyway? I see no point of unset in a scripting language with garbage collection.
-
In the below example, we match 0 or more alphanumeric characters in the beginning and 0 or more alphanumeric characters in the end. Then we create 4 groups. Each group has "?=" which means "the next text must be like this". Now in first group we match any character 8 or more times. The next group we match 0 or more characters and a digit. In the next group we match 0 or more characters and a lowercase letter. In the next group we match 0 or more characters and an uppercase letter. <?php $password = "Fyfjk34sdfjfsjq7"; if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) { echo "Your passwords is strong."; } else { echo "Your password is weak."; } ?> My question is do these four groups impact each other? That is, the fact that the first group has 8 or more characters means that all the groups must have 8 or more characters. The fact that the second group has a digit means that all the groups must have a digit. Or do they work exclusively meaning that a 4 character word with a single digit would match this pattern, even though first group says it must have 8 characters.
-
I find it a bit buggy that strpos returns false instead of -1. The equivalent C implementation returns -1: int strpos(char s[], char t[]) { int i, j, k; for (i = 0; s[i] != '\0'; i++) { for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++) ; if (k > 0 && t[k] == '\0') return i; } return -1; } The problem with returning false is that false can also evaluate to 0 which can also happen to be the first index of a match. Therefore, you have a hard to find bug. I know that you can use the identity operator to check for falseness: ===. But wouldn't it be a better design for the function to just return -1?
-
Arrays in C are fixed length: #define MAX_SIZE 5 int main(void){ int n[ 5 ]; for(int i=0;i<MAX_SIZE;i++){ n[i]=i; } return 1; } If I try to insert an eleement into the array, it will result in undefined behavior. Java compensates for this by creating an actual object class called ArrayList or LinkedList, the former an internal store of array elements that dynamically resizes and the latter just a node tree with pointers to next and previous elements. ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(1); arr.add(2); It seens that the array() construct in PHP is not a function or a class but an internal construct. How does it dynamically resize arrays?
-
Why is binary mode missing from fopen() modes?
johnmerlino1 replied to johnmerlino1's topic in PHP Coding Help
Well, I linked the manual to the original question and that actually indeed was my question - why is this mode missing and since it is missing, is it not needed anymore in PHP5? -
I have the following code: $fp = fopen(“path_to_file”, ‘a’); flock($fp, LOCK_EX); fwrite($fp, $string); flock($fp, LOCK_UN); fclose($fp); If I try to lock the file in two different places at the same time, this will cause a race condition. How can I prevent this? I know in Java, for example, it has a concurrent library which contains reentrant lock, which basically tries to get the lock and if can't waits. What can I do in PHP?
-
Why is binary mode missing from fopen() modes?
johnmerlino1 replied to johnmerlino1's topic in PHP Coding Help
But if I want to open a file for reading, can I just use r or do I need to attach b as well? $fp = fopen(“$DOCUMENT_ROOT/../orders/orders.txt”, ‘rb’); -
I was looking at the manual: http://php.net/manual/en/function.fopen.php And I noticed the the binary mode 'b' is missing from the list. Any reason why?
-
I'm trying to use the executor operator to print a list of all files in my home directory onto the browser. I am using Ubuntu 12.04 and therefore I am using standard unix commands. Unfortunately, it doesn't print anything to the browser. But I don't even understand how this is supposed to work. Which user in /etc/passwd is the actual commands running as? How does PHP know which system user to run the commands as? $out = `cd ~ && ls -l`; echo '<pre>'.$out.'<pre>';
-
date function displays the wrong time
johnmerlino1 replied to johnmerlino1's topic in PHP Coding Help
You I think you are right. I was displaying seconds not minutes. -
date function displays the wrong time
johnmerlino1 replied to johnmerlino1's topic in PHP Coding Help
I'm running this on localhost on Ubuntu 12.04 desktop, using php5 and apache2 from the repositories (apt-get). -
I have following script: <?php $tireqty = isset($_POST['tireqty']) ? $_POST['tireqty'] : null; $oilqty = isset($_POST['oilqty']) ? $_POST['tireqty'] : null; echo "<p>order processed on " . date("F d, Y g:sA") . "</p>"; if($tireqty){ echo "tire quantity " . $tireqty; } if($oilqty){ echo "oil quantity " . $oilqty; } echo <<<theEnd Thank you for visiting our store and buying $tireqty tires and 3 $oilqty. Have a very nice day. theEnd; ?> Aptana shows error for the last "?>" as soon as I add the heredocs and when running it in browser, I get the following error: Parse error: syntax error, unexpected $end in /home/myuser/public_html/shopwithus.com/controllers/orders_controller.php on line 20 What might I be doing wrong?
-
First, I would like to say when i tried to recover my account from this website, I took me 10 attempts to get the captcha right and then finally it said it sent me an email to my gmail account. I checked spam folder and everything there was no such email from this site. Then I decided to create a new account, well, it took me another 10 attempts to get the captcha right and finally when it was submitted, the page was loading for around 3 minutes before it signed me in. My question is about the php date() function. It accepts a format to display a time. In the following example I use F for full representation of month, d for 2-digit day of month with leading zeros, Y for full year, g for 12-hour format without leading 0s, s for seconds and A for meridiem. It uses the correct format, but it gives me the wrong time. My local time is 4:43 and it prints out 4:12: <?php echo "<p>order processed on " . date("F d, Y g:sA") . "</p>"; ?> Why is it 30 minutes behind?