I have thos piece of code:
Math&&Math.random?Math.floor(Math.random()*10000000000000):Date.getTime();
And as far as i know && is logic operator for AND, so im trying to convert this into PHP and this is where i got:
intval(floor(time()/10800000)%10+(rand()?floor(rand()*10000000000000):time()))
The problem is that i can't understand the first part
Math&&
Can anyone help with this one cause i always get negative result, when i should get positive (probably the logic rand-time is not working in my php example)
That's a test to make sure the Math class is available in the browser. You don't need that in PHP. The second clause checks to make sure the Math.random method is available. It uses it if it is, and uses the time if it is not.
In PHP, just use rand(). http://us2.php.net/rand
It may just be the ternary operator that is throwing you off.
http://en.wikipedia.org/wiki/Ternary_operation
It's a test to see if the Math variable is non-null/undefined. I've never seen it before, but I assume it's to prevent an error if, for some reason, Math is null (although, I can't imagine why that would happen).
There, your Javascript code is :
Testing if the Math object exists
and has a random method
If yes, using it to generate a random value
else, using the date as fallback to get a random value
In you PHP code, you know that rand() and mt_rand() exist ; no need to test if they do ;-)
Which means the tests and condition are useless, and that you don't need to re-code them in PHP : just keep the part between the ? and the : in the original code.
Related
We are looking for a PHP linter, and we are chasing a particular problem that's causing E_NOTICE's, a lot of them:
if($undef_variable)...
if($assoc['undef_key'])...
$undef_variable?...:...
$assoc['undef_key']?...:...
Functionally, the code works perfectly so if the tools was also able to replace on-the-fly such occurences with i.e.
if($undef_variable??null)
That would be a huge help.
Some of the code is in templates that are in included with some pre-set variables (always the same). So ideally the tool would also allow configuring some available global-namespace variables.
The tool should absolutely understand PHP7 syntax, especially anonymous functions.
At a minimum, we need to generate a list of every occurence where a variable is used as a boolean condition and is not defined in the same scope, and every occurence where an array key is used as a boolean condition.
Phpcs- PhpCodeSniffer can be used for that. You need to configure a rule for that. Find it here - Phpcs
Check this rule -
https://github.com/sirbrillig/phpcs-variable-analysis
If you use PhpStorm, it has an option in Inspections. https://www.jetbrains.com/help/phpstorm/php-undefined-variable.html
My question is about the PHP functions for manipulating array elements, like array_pop() and array_shift().
On all examples I've seen (including php.net), since those functions return the value being removed, they are assigned to a variable when executed, for example:
$exampleArray=array("1","2","3");
$removedNum=array_pop($exampleArray);
What I can't find is whether you have to assign the removed value or could you just pop the value from the end and be done with it, like in Ruby, for example.
I have tried and it works, e.g.:
array_pop($exampleArray);
but I'm not sure if this is an acceptable practice in PHP programming? Or should I always assign the value to a variable?
It is valid to use array_pop() and array_shift() to remove unwanted values, and in some cases, can even make sense depending on the data that you're working with.
I.e., if you are working with CSV files, and have an array of lines from that file, where the first line is header data that you know will never change (a bold assumption), and that does not matter to your script, you can safely remove that first line from your array before starting the loop to process the values.
As for whether that's a good practice or not, that's something to discuss with the people maintaining your code...
You can just:
array_pop($exampleArray);
You don't have to assign the value to a variable if you don't need to use it.
It depends on what your application is doing. For example if your application really needs to save the last number being popped then yes. Otherwise, you don't need the variable. Furthermore, It takes memory to create a variable. It may not seem as much for small operations but if you have a loop of a billion operations, then this becomes wasteful. As long as you code is readable, you'll be fine :).
One of our partner websites is sending through a tracking field on our link and the query string looks like this:
?tracking=value%u200B
When "value" gets looked up in our DB via PHP PDO, it kills the query (fatal error). I'd have thought prepared statements would cope with this but I guess not!
How can I pick up any codes like this during the initial hit on my website to keep the strings clean?
This has to be better than simply asking them to fix the URL in case other site do the same.
There are many ways to do it. If you know your value pattern you can do preg_replace to clean out unwanted string. If you know your messy value always starts with % and your actual value never contains % then you can do something similar to below.
You should also check with your partner website so that they will not send you anything undesired.
echo substr($_GET['tracking'], 0, strpos($_GET['tracking'], '%'));
I'm building a web application in PHP, and part of the requirement is that I need to be able to quickly process data on a scanned copy of a fairly simple form, and save it to a database for later retrieval.
Given the following image
how can I identify and assign a database field a value of either true or false (true when it sees a tick, and false otherwise)?
I'm thinking along the following line of implementation:
I will keep two copies of the above image - the first will have ticks shown (as above), and the second will be a "clean" copy of the image with the borders left behind. Comparing between the two images will yield a difference; the difference will return either a value of true or false.
There are drawbacks as far as I can observe of the above implementation. What happens if the user scribbles something in it (as seen above) but it does not mean anything? How do I even ensure that the returned values of true or false are assigned to the appropriate columns in the database?
I don't have any code implementation at this point in time, and I'm not asking for it. Rather, I'm asking for guidance on where to look and how I can efficiently do this.
You may try using OpenCV framework for PHP (https://github.com/mgdm/OpenCV-for-PHP, http://mgdm.net/talks/confoo11/making-php-see.pdf) and use contour detection (or any other classificators) to find signs like "V" and skip false-positives.
You might want to use a PHP OCR library.
I will do thin in a following way: I will divide image into 2x6 grid and count black pixels in each row. If the number n contains in <A;B> then we can assume that row is checked. If someone scratch off an answer then n is larger than B.
So if n is in <A;B> range we can check its pattern - for example common part of the all marked rows because of user's handwriting.
I'm not sure how to really word this. I've been getting into PHP CLI lately, and I wanted to make like a dynamic counter. For example, it starts with echoing Count: 0 and then as the count increases (or decreases) the number changes as the program advances.
I hope you can understand what I mean. If you do, any idea on if its possible, and if so, how to do it?
As I understand you question, you don't wan't to repeatedly echo "Count: N", but you want it to remain on the screen and that only the number changes.
For this you must use the PHP extension Ncurses - http://si2.php.net/ncurses
It's basically a wrapper for the Ncurses terminal control library. It's quite powerful but not so easy to learn.
One way is to create a file somewhere in a directory like:
/files/counter.txt
Start it with a single number 0,
then every time you want to change it, read the file in
and update it. Since the only content in the file
is a number, just increment it.