-
Posts
2,134 -
Joined
-
Last visited
-
Days Won
42
Posts posted by benanamen
-
-
2 hours ago, Psycho said:
The OP never stated "where" the value is coming from.
Which is why you cant say "@Barand's test makes more sense." Based on OP's code, he expects there could be a negative int, otherwise he wouldn't be checking for it.
If "int" is negative, @Barands solution will fail (false).
var_dump(ctype_digit('-128')); // False
Casting the string to an int wouldn't work either.
$x = (int) '5';
Yes , it will make it an int and support negative values, but it will cast "anything" you put in there into an int such as the following and then you cannot check if it is a real int.
$x = (int) 'xx'; // int 0
So what to do then? One option that also addresses the source as a string argument would be the following.
function fact($x): int { return $x <= 1 ? 1 : $x * fact($x - 1); } $x = '-2147483648';// OK $x = -2147483648;// OK $x = 5; // OK $x = '5';// OK $x = 'ABC'; // Fail: Please provide an integer echo (filter_var($x, FILTER_VALIDATE_INT) === 0 || filter_var($x, FILTER_VALIDATE_INT)) ? fact($x) : 'Please provide an integer';
-
Hi @Barand,
I am wondering why you would be introducing strings when this is specifically about int's. IMO is_int should be used instead of ctype_digit.
OP, the else is redundant. You can remove it. Additionally, IMO the int check should not be in the function. The function/method should do one thing and do it well.
As is example
function fact($x) { if (!is_int($x)) { return 'Please provide an integer'; } if ($x <= 1) { return 1; } return $x * fact($x - 1); } echo fact(5);
Suggested Example
function fact(int $x): int { return $x <= 1 ? 1 : $x * fact($x - 1); } $x = 5; echo is_int($x) ? fact($x) : 'Please provide an integer';
-
As to the specific error, you need to add quotes like so
getenv('QUERY_STRING')
Additionally, just about everything that could be wrong with this code is wrong.
-
@johnwalsh1020,
This thread died over a year ago and the OP is long gone. Besides that, the tutorial you linked to is poorly coded and a bad example of how to do login/reg code. md5 for passwords should have been your first red flag.
-
I am surprised there has been no mention of foreign key constraints.
OP, if you set up proper foreign key relationships, all you would have to do is delete the parent record using ON DELETE CASCADE and all the child records would automatically delete as well.
Here is some information about it.
-
-
3 hours ago, Barand said:
Not necessarily
When it comes to the forums I have never seen it not be.
OP, tell us about what you actually have going on rather than asking about your attempted solution to it.
-
Why do you have duplicate table structures? This points to a design flaw.
-
PRG - Post, Redirect, Get
-
9 hours ago, kicken said:
For example if there were a file named "0" then the loop would incorrectly stop at that file.
One of the gotcha's I was unaware of. Thanks.
-
5 hours ago, SaranacLake said:
!== FALSE)
Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr).
-
8 hours ago, clemente said:
I want URL to be shorter
It may be beyond your skills, but If that is the goal you can write a router that will call whatever file you want regardless of the name and URL. Look into MVC design.
-
Yes but you don't want to run both at the same time. If you really wanted to, you would need to change the Apache port on one of them as they both use port 80
- 1
- 1
-
4 hours ago, thehubclick said:
This xampp is the best
You would find Laragon to be even better. (Coming from a former Xampp user)
-
3 hours ago, tryingphp said:
I think this will need some sort of session id.
You will need a session id and a WHERE clause in your query.
-
For starters, you don't need to check if the session has been started. Just set it and be done with it. You also need to kill the script after a header redirect and don't create variables for nothing.
-
The first thing you do is issue a redirect and kill the script. How do you expect it to do anything after that?
-
OP, I would really suggest you actually learn how to code instead of using a WYSIWYG. The output is absolutely horrendous.
-
Op was given the answer on another forum
-
27 minutes ago, platzDB said:
It has been suggested
What should be suggested is that you stop using dangerous obsolete mysql code that has been completely removed from PHP and was warned about for well over ten years and to stop running a php version that is hundreds of releases behind and reached end of life long ago. Toss that code in the trash. Every bit of it is bad.
You need to use PDO with Prepared Statements. This tutorial will get you going.
-
OP, you were handed a complete properly coded example on the other forum by a gracious expert. Why are you still messing around with this bad code?
-
19 minutes ago, ManieE said:
Not sure what this means...
It means you posted this in multiple forums and someone already took the time in another forum to answer you so we are not going to waste more experts time answering something that has already been answered.
- 1
-
OP was given answer in another forum.
-
Money is not an int.
Should the request hold the response, the response hold the request, or both (or neither)??
in PHP Coding Help
Posted
A quick observation....
You have tightly coupled the Request and Response Classes. It would be better to use Dependency Injection (DI) to give the class what it needs by passing the Response instance to the Request Class.