How to prevent implicit type-conversion in PHP? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

How to prevent implicit type-conversion in PHP?

I already tried "declare(strict_types=1);" at the very top of the script which is supposed to do the job, but it throws an error telling me that the strict_types declaration must be the first statement in the code. Please have a look at the code for a better understanding. https://code.sololearn.com/wv50ns6LzM1I/?ref=app (Thank you Ipang for your patience!)

25th Aug 2018, 2:53 AM
Kurt Sicht
Kurt Sicht - avatar
5 Answers
+ 4
I also thought it would, but unfortunately it results in the following error message: "Fatal error: strict_types declaration must be the very first statement in the script"
26th Aug 2018, 1:01 PM
Kurt Sicht
Kurt Sicht - avatar
+ 3
Dababi Mohamed Amine, for some reason it doesn't work for me, but thank you very much for your feedback.
27th Aug 2018, 2:49 AM
Kurt Sicht
Kurt Sicht - avatar
+ 2
Hi, Strict_types was added in php7. It takes either 1 or 0. If 1, strict type-checking mode is used for function calls and return statements in this file.  But you must put it in the first line in your code like this (there is no semicolon): <?php declare(strict_types=1) then your code will run.
26th Aug 2018, 7:17 AM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 2
the first line should be <?php declare(strict_types=1) php and declare must be in the same line
26th Aug 2018, 10:46 PM
Dababi Mohamed Amine
Dababi Mohamed Amine - avatar
+ 1
Actually there is a problem in editor. what u have to do is to copy that code paste it into notepad and than save the file with ANSI encoding. then try to run it hopefully the problem would be resolved
2nd Feb 2021, 3:06 PM
Muzamil Hussain
Muzamil Hussain - avatar