php empty 関数ではまった

このエントリーは IT エンジニア向けです。

何の因果か、最近 php を書いている。最後に php 書いたのいつだろう?確か iPhone アプリのサーバーサイドを実装した時かな。もう金輪際 php は使いたくないと思った日々が懐かしい。まあ、最近は type hint もついたし、php も昔ほど嫌いじゃないけどね。

そんな中、久しぶりに php 書いたら若干ハマったことが合ったので書いてみる。

俺は Bitcoin に代表される暗号通貨関連の仕事をしている分けだが、残念な事に php では関連するライブラリがまだ少ない。どっかのオープンソースから拝借しようかとも思ったが、php 製の有名なオープンソースも見つからなかった。(探せば有るんだが、わざわざソースをハックする信頼性や価値が有るとは思えなかった。)

ってな分けで、俺の感覚ではどっかのライブラリ使いたくなるような下回りのメソッドも今回は自力で実装するハメに。とはいえ、俺自身、暗号通貨についての知識はまだ浅いし中、下回りの実装は勉強にもなるので悪いことばかりではない。

半分イヤイヤ、半分ウキウキでライブラリの実装を行った。

ところが、このライブラリを使った環境で負荷検証でポチポチエラーが出てくる。エラーの原因は負荷ではないっぽい。プログラムのバグだ。

「エッヂケースかな?」と思い、色々な境界値を入れてテストしてみたが、正常に動く。分からん。

至る所に var_dump を入れ print デバッグを行っていると、巨大整数の取り扱いでエラーが起こっているような気がしてきた。

ご存知の方も多いかもしれないが、php ではネイティブで巨大な整数が取り扱えない。俺の環境では、整数は 64 bit の固定長だった。64 bit ではとても足りなかったので自前で巨大整数を扱う関数を作ったのだ。そこでエラーが発生しているらしい。

ただ、正直なところ最初は信じられなかった。

ここでエラーが起きると後で面倒な事は分かりきっていたので、テストも少し入念に行った。一方、これらの機能は副作用が無いのでテストはどれも簡単だ。それなのに、本当にここでエラーが起きるのか?

半信半疑で再度色々とやってみると、驚きの結果が。

なんと、48 という数字を用いた時だけ四則演算(足し算、引き算、掛け算、割り算)の結果がおかしくなっていたのだ。何でよりによって 48 ?全部エラーなら分かる。0, 1, 255, 256 というコンピューター的に特別な値だけでエラーが起こるのも、分かる。でも何で 48 ?

完全に混乱しながらデバッグを続けて、やっとわかった。

php 組み込みの empty 関数だ。

empty はデータが空かどうかを判断する。例えば、空の array や空の string を渡すと TRUE を返し、それ以外の場合は FALSE を返す。

と、思っていた。

だが実際には、少し例外が合ったようだ。

‘0’ という文字列 (数字の 0 を表す文字列) を渡した時も empty は TRUE を返す。

俺は大きい整数を扱うために整数を byte 列に変換し、string として保存していた。そして、その string が空の場合は数字の 0 として扱うよう、empty を使って文字列をチェックしていた。ところが 48 という数字を byte 列にすると php が文字列の ‘0’ と解釈し、empty が俺の意図に反する挙動を示したのだ。(‘0’ の ascii コードは 48)

つまり、48 という数字が所々で 0 に変換されていたため、計算結果がおかしくなっていた。php プログラマの間では常識なんだろうけど、知らなかった。

php の型は超弱い事は知っていた。だからこそハマるとしたらこういう所だと思って注意していたのに、それでも落とし穴を踏みぬいてしまった。なんか悔しい。

Author: wbcchsyn

未来のスーパーエンジニア とりあえず、新しい物が大好き

105 thoughts on “php empty 関数ではまった”

  1. I just want to say I am just new to weblog and honestly enjoyed this web site. More than likely I’m planning to bookmark your blog . You surely have wonderful posts. Thanks for revealing your web-site.

  2. I was just searching for this info for a while. After 6 hours of continuous Googleing, at last I got it in your website. I wonder what’s the lack of Google strategy that do not rank this kind of informative sites in top of the list. Normally the top web sites are full of garbage.

  3. You actually make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I’m looking forward for your next post, I will try to get the hang of it!

  4. May I just say what a comfort to discover someone who actually knows what they’re talking about on the net. You certainly understand how to bring a problem to light and make it important. A lot more people must look at this and understand this side of the story. I was surprised that you aren’t more popular given that you most certainly possess the gift.

  5. My brother recommended I might like this web site. He was totally right. This post truly made my day. You can not imagine simply how much time I had spent for this info! Thanks!

  6. I like what you guys are up too. Such clever work and reporting! Keep up the excellent works guys I¡¦ve incorporated you guys to my blogroll. I think it will improve the value of my web site 🙂

  7. You could definitely see your enthusiasm within the paintings you write. The arena hopes for even more passionate writers such as you who are not afraid to say how they believe. At all times go after your heart.

  8. you’re really a good webmaster. The site loading speed is incredible. It seems that you are doing any distinctive trick. Moreover, The contents are masterwork. you’ve done a wonderful process on this subject!

  9. I would like to voice my passion for your generosity supporting individuals who have the need for guidance on that idea. Your special dedication to passing the message up and down turned out to be definitely productive and has continually enabled some individuals just like me to arrive at their pursuits. Your amazing invaluable guidelines indicates a lot a person like me and somewhat more to my mates. Warm regards; from each one of us.

  10. Hey there, You’ve done a great job. I’ll certainly digg it and personally recommend to my friends. I am sure they’ll be benefited from this site.

  11. Very nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed surfing around your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again very soon!

  12. I am extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it’s rare to see a nice blog like this one nowadays..

  13. I needed to put you one bit of word just to say thanks over again on the nice principles you’ve provided on this website. This is quite strangely open-handed with you to grant publicly what exactly numerous people would have advertised for an ebook to make some dough for themselves, mostly considering the fact that you could possibly have tried it if you desired. The good tips in addition acted like the good way to recognize that many people have the identical desire similar to mine to understand many more when it comes to this problem. Certainly there are numerous more pleasurable occasions up front for those who browse through your website.

  14. I just wanted to type a remark to thank you for the marvelous recommendations you are posting at this site. My extended internet search has at the end of the day been paid with really good facts and techniques to go over with my companions. I ‘d believe that many of us visitors actually are very much lucky to dwell in a fantastic site with many perfect individuals with good principles. I feel pretty grateful to have discovered the website and look forward to tons of more awesome times reading here. Thank you once more for a lot of things.

  15. Thank you for your own hard work on this website. Betty takes pleasure in participating in investigations and it’s really easy to see why. Most of us notice all relating to the dynamic means you give worthwhile solutions via this web blog and as well boost participation from people on this idea so my girl is in fact understanding so much. Take advantage of the rest of the new year. You are always conducting a dazzling job.

  16. I wish to convey my passion for your kindness in support of folks that actually need help on this particular question. Your real dedication to passing the solution all through had been definitely practical and has surely enabled regular people much like me to attain their desired goals. Your own warm and friendly tips and hints indicates this much a person like me and substantially more to my fellow workers. Best wishes; from everyone of us.

  17. This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your fantastic post. Also, I’ve shared your web site in my social networks!

  18. Fantastic goods from you, man. I have understand your stuff previous to and you are just too excellent. I actually like what you’ve acquired here, really like what you are saying and the way in which you say it. You make it enjoyable and you still take care of to keep it smart. I can not wait to read much more from you. This is really a terrific web site.

  19. Some genuinely great articles on this internet site, appreciate it for contribution. “I finally know what distinguishes man from other beasts financial worries. – Journals” by Jules Renard.

  20. excellent issues altogether, you simply received a brand new reader. What may you suggest about your post that you just made some days ago? Any sure?

  21. F*ckin’ remarkable things here. I’m very glad to look your post. Thank you a lot and i’m having a look forward to touch you. Will you kindly drop me a e-mail?

  22. I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!

  23. Its excellent as your other content : D, regards for posting . “The real hero is always a hero by mistake he dreams of being an honest coward like everybody else.” by Umberto Eco.

  24. Its great as your other blog posts : D, thankyou for putting up. “A lost battle is a battle one thinks one has lost.” by Ferdinand Foch.

  25. certainly like your web-site but you need to test the spelling on several of your posts. Several of them are rife with spelling issues and I in finding it very troublesome to inform the truth on the other hand I’ll surely come back again.

  26. I do agree with all of the concepts you’ve introduced to your post. They are very convincing and can certainly work. Still, the posts are too brief for beginners. May just you please extend them a bit from subsequent time? Thank you for the post.

  27. Fantastic site. A lot of helpful information here. I am sending it to some pals ans additionally sharing in delicious. And of course, thanks in your effort!

  28. Thanks so much for giving everyone an exceptionally terrific chance to read from this website. It is always so pleasurable plus jam-packed with amusement for me personally and my office fellow workers to visit your site particularly three times in 7 days to read through the new guidance you will have. And indeed, I am also certainly happy with your incredible inspiring ideas served by you. Selected 1 ideas on this page are in reality the best we have ever had.

  29. I have not checked in here for some time since I thought it was getting boring, but the last several posts are good quality so I guess I’ll add you back to my everyday bloglist. You deserve it my friend 🙂

  30. I like what you guys are up also. Such clever work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my site :).

  31. What i do not realize is actually how you are no longer really a lot more neatly-liked than you might be now. You’re very intelligent. You know thus significantly on the subject of this subject, made me for my part believe it from so many varied angles. Its like women and men are not involved except it’s something to accomplish with Girl gaga! Your own stuffs excellent. Always handle it up!

  32. I was just searching for this info for a while. After 6 hours of continuous Googleing, finally I got it in your site. I wonder what’s the lack of Google strategy that do not rank this type of informative sites in top of the list. Normally the top sites are full of garbage.

  33. I wish to show some thanks to you for bailing me out of this particular circumstance. Because of researching through the the web and coming across ideas which are not powerful, I thought my entire life was gone. Being alive minus the approaches to the problems you’ve solved as a result of this posting is a serious case, as well as the ones that might have badly damaged my entire career if I had not noticed your site. Your main expertise and kindness in controlling a lot of stuff was very helpful. I am not sure what I would have done if I hadn’t encountered such a subject like this. I am able to at this moment look forward to my future. Thank you very much for this impressive and result oriented guide. I won’t hesitate to endorse your web site to anyone who requires recommendations on this topic.

  34. Hello, Neat post. There’s a problem together with your site in web explorer, would test this… IE nonetheless is the market chief and a big portion of other folks will leave out your wonderful writing because of this problem.

  35. Very interesting info !Perfect just what I was looking for! “Peace, commerce and honest friendship with all nations entangling alliances with none.” by Thomas Jefferson.

  36. Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Fantastic. I am also a specialist in this topic so I can understand your effort.

  37. Someone essentially help to make significantly posts I’d state. That is the first time I frequented your web page and up to now? I surprised with the analysis you made to make this actual put up incredible. Fantastic process!

  38. Pretty nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed surfing around your blog posts. In any case I will be subscribing to your feed and I hope you write again soon!

  39. I like this post, enjoyed this one regards for putting up. “The goal of revival is conformity to the image of Christ, not imitation of animals.” by Richard F. Lovelace.

  40. You can certainly see your skills within the paintings you write. The sector hopes for more passionate writers like you who aren’t afraid to mention how they believe. At all times follow your heart. “A simple fact that is hard to learn is that the time to save money is when you have some.” by Joe Moore.

  41. I really like your writing style, excellent info, regards for putting up :D. “He wrapped himself in quotations- as a beggar would enfold himself in the purple of Emperors.” by Rudyard Kipling.

  42. excellent points altogether, you simply received a new reader. What might you suggest in regards to your publish that you simply made a few days in the past? Any certain?

  43. I will immediately clutch your rss feed as I can not find your e-mail subscription link or e-newsletter service. Do you’ve any? Please allow me recognize so that I could subscribe. Thanks.

  44. I have learn several excellent stuff here. Definitely value bookmarking for revisiting. I wonder how so much effort you place to make any such wonderful informative web site.

  45. Thank you, I have just been looking for info approximately this subject for a while and yours is the greatest I’ve discovered till now. However, what concerning the conclusion? Are you positive in regards to the source?

  46. Magnificent site. A lot of useful information here. I’m sending it to some pals ans also sharing in delicious. And of course, thanks to your sweat!

  47. I do consider all the ideas you have presented for your post. They’re really convincing and can definitely work. Nonetheless, the posts are very brief for novices. May just you please prolong them a bit from next time? Thank you for the post.

  48. Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is excellent blog. A great read. I will definitely be back.

  49. I just could not go away your website before suggesting that I actually enjoyed the usual info an individual provide to your guests? Is going to be again steadily to check up on new posts.

  50. What i do not realize is in reality how you’re not actually much more neatly-preferred than you may be now. You’re very intelligent. You realize therefore significantly in terms of this subject, made me for my part believe it from numerous varied angles. Its like women and men aren’t fascinated until it is something to do with Lady gaga! Your individual stuffs excellent. Always maintain it up!

  51. I want to show appreciation to you just for rescuing me from such a trouble. After researching through the online world and obtaining things which were not powerful, I figured my entire life was done. Living devoid of the solutions to the difficulties you have sorted out all through your good short article is a serious case, as well as those that could have in a wrong way affected my career if I had not discovered your website. Your main knowledge and kindness in touching every part was priceless. I am not sure what I would have done if I hadn’t come across such a stuff like this. I am able to at this point relish my future. Thank you so much for this expert and effective help. I will not be reluctant to endorse your blog post to anyone who needs counselling about this problem.

  52. Thanks so much for providing individuals with a very nice chance to read critical reviews from here. It is always very pleasant plus packed with fun for me and my office fellow workers to visit your web site the equivalent of thrice per week to find out the fresh tips you have. And indeed, I’m so certainly astounded considering the fantastic concepts served by you. Some two points on this page are indeed the most impressive I have ever had.

  53. F*ckin’ awesome issues here. I’m very glad to look your post. Thanks so much and i am taking a look forward to contact you. Will you kindly drop me a e-mail?

  54. Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Actually Fantastic. I’m also a specialist in this topic therefore I can understand your effort.

  55. My brother suggested I might like this blog. He was entirely right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  56. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?

  57. I’ve read several just right stuff here. Definitely worth bookmarking for revisiting. I surprise how so much attempt you put to make this type of excellent informative web site.

  58. Hello, you used to write great, but the last few posts have been kinda boring… I miss your great writings. Past several posts are just a little out of track! come on!

  59. I like this web blog very much, Its a really nice situation to read and find information. “The love of nature is consolation against failure.” by Berthe Morisot.

  60. Very interesting points you have remarked, regards for putting up. “Above all be true to yourself, and if you can not put your heart in it, take yourself out of it.” by Hardy D. Jackson.

  61. I and my pals have been checking out the nice recommendations from the website and at once developed an awful suspicion I had not thanked the web blog owner for those tips. The men are actually so warmed to study all of them and already have clearly been enjoying those things. Many thanks for getting so helpful as well as for figuring out varieties of tremendous subjects most people are really eager to be informed on. My personal honest regret for not expressing gratitude to earlier.

Leave a Reply

リンクの無いコメントは即時反映されます