{"id":744,"date":"2025-01-04T22:03:06","date_gmt":"2025-01-04T14:03:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=744"},"modified":"2025-01-04T22:03:06","modified_gmt":"2025-01-04T14:03:06","slug":"%e5%9c%a8-php-%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%88string%ef%bc%89%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b4%e6%95%b0%ef%bc%88int%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/04\/%e5%9c%a8-php-%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%88string%ef%bc%89%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b4%e6%95%b0%ef%bc%88int%ef%bc%89\/","title":{"rendered":"\u5728 PHP \u4e2d\uff0c\u600e\u6837\u5c06\u5b57\u7b26\u4e32\uff08string\uff09\u8f6c\u6362\u4e3a\u6574\u6570\uff08int\uff09"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 PHP \u4e2d\uff0c<strong>\u5c06\u5b57\u7b26\u4e32\uff08<code>string<\/code>\uff09\u8f6c\u6362\u4e3a\u6574\u6570\uff08<code>int<\/code>\uff09<\/strong>\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e0b\u9762\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u8f6c\u6362\u65b9\u5f0f\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\uff08Casting\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5c06\u53d8\u91cf\u5f3a\u5236\u8f6c\u6362\u4e3a <code>int<\/code> \u7c7b\u578b\uff0cPHP \u4f1a\u81ea\u52a8\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6574\u6570\u3002\u6570\u5b57\u5b57\u7b26\u4e32\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u6574\u6570\uff0c\u800c\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u5219\u4f1a\u88ab\u8f6c\u6362\u4e3a 0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   $str = \"123\";\n   $int = (int)$str;\n   echo $int;  \/\/ \u8f93\u51fa 123\n\n   $str2 = \"abc\";\n   $int2 = (int)$str2;\n   echo $int2;  \/\/ \u8f93\u51fa 0<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>intval()<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>intval()<\/code> \u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u8f6c\u6362\u7684\u57fa\u6570\uff08\u9ed8\u8ba4\u4e3a 10\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bed\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   intval(string $var, int $base = 10): int<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$var<\/code>\uff1a\u8981\u8f6c\u6362\u7684\u503c\u3002<\/li>\n\n\n\n<li><code>$base<\/code>\uff1a\u53ef\u9009\uff0c\u8f6c\u6362\u7684\u8fdb\u5236\uff08\u9ed8\u8ba4\u4e3a 10\uff09\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   $str = \"123\";\n   $int = intval($str);\n   echo $int;  \/\/ \u8f93\u51fa 123\n\n   $str2 = \"1011\"; \/\/ \u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\n   $int2 = intval($str2, 2);\n   echo $int2;  \/\/ \u8f93\u51fa 11<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f<\/strong>\uff1a\u5982\u679c\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u662f\u975e\u6570\u5b57\u5b57\u7b26\uff0c<code>intval()<\/code> \u4f1a\u628a\u5b83\u4ece\u5de6\u5230\u53f3\u8fdb\u884c\u8f6c\u6362\uff0c\u76f4\u5230\u9047\u5230\u975e\u6570\u5b57\u5b57\u7b26\u4e3a\u6b62\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>(int)<\/code> \u5f3a\u5236\u8f6c\u6362 + <code>filter_var()<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u7ed3\u5408 <code>filter_var()<\/code> \u4f7f\u7528\uff0c\u7528\u4e8e\u66f4\u4e25\u683c\u7684\u8fc7\u6ee4\u548c\u8f6c\u6362\u3002<code>filter_var()<\/code> \u5141\u8bb8\u4f60\u6307\u5b9a <code>FILTER_VALIDATE_INT<\/code> \u8fc7\u6ee4\u5668\u6765\u9a8c\u8bc1\u548c\u8f6c\u6362\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bed\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   filter_var(string $variable, int $filter, array $options = ?)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$filter<\/code>\uff1a<code>FILTER_VALIDATE_INT<\/code> \u7528\u4e8e\u9a8c\u8bc1\u6574\u6570\u3002<\/li>\n\n\n\n<li><code>$options<\/code>\uff1a\u53ef\u9009\uff0c\u8bbe\u7f6e\u9009\u9879\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   $str = \"123\";\n   $int = filter_var($str, FILTER_VALIDATE_INT);\n   echo $int;  \/\/ \u8f93\u51fa 123\n\n   $str2 = \"abc\";\n   $int2 = filter_var($str2, FILTER_VALIDATE_INT);\n   echo $int2;  \/\/ \u8f93\u51fa false\uff08\u8bf4\u660e\u4e0d\u662f\u6709\u6548\u6574\u6570\uff09<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong><code>+0<\/code>\uff08\u52a0\u96f6\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32\u4e0e <code>0<\/code> \u76f8\u52a0\uff0cPHP \u4f1a\u81ea\u52a8\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u4f46\u8981\u6ce8\u610f\uff0c\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u4f1a\u88ab\u8f6c\u6362\u4e3a 0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   $str = \"123\";\n   $int = $str + 0;\n   echo $int;  \/\/ \u8f93\u51fa 123\n\n   $str2 = \"abc\";\n   $int2 = $str2 + 0;\n   echo $int2;  \/\/ \u8f93\u51fa 0<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong><code>settype()<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>settype()<\/code> \u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5f3a\u5236\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\u3002\u5728\u8fd9\u91cc\uff0c\u53ef\u4ee5\u5c06\u53d8\u91cf\u8bbe\u7f6e\u4e3a <code>int<\/code> \u7c7b\u578b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bed\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   settype($var, \"integer\");<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$var<\/code>\uff1a\u8981\u8f6c\u6362\u7684\u53d8\u91cf\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   $str = \"123\";\n   settype($str, \"integer\");\n   echo $str;  \/\/ \u8f93\u51fa 123<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong><code>(int)\"\"<\/code> \u6216 <code>intval()<\/code> \u5bf9\u7a7a\u5b57\u7b26\u4e32\u7684\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff0c\u6240\u6709\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u4f1a\u8fd4\u56de <code>0<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   $str = \"\";\n   $int = (int)$str;\n   echo $int;  \/\/ \u8f93\u51fa 0\n\n   $str2 = \"\";\n   $int2 = intval($str2);\n   echo $int2;  \/\/ \u8f93\u51fa 0<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f3a\u5236\u8f6c\u6362 <code>(int)<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong><code>intval()<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u6574\u6570\uff0c\u652f\u6301\u8fdb\u5236\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong><code>filter_var()<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u66f4\u4e25\u683c\u7684\u9a8c\u8bc1\u548c\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong><code>+0<\/code><\/strong>\uff1a\u6700\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u4f46\u5bf9\u4e8e\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u4f1a\u8f6c\u6362\u4e3a <code>0<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>settype()<\/code><\/strong>\uff1a\u5c06\u53d8\u91cf\u7684\u7c7b\u578b\u4fee\u6539\u4e3a <code>int<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u6839\u636e\u4f60\u7684\u9700\u6c42\u6765\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u6574\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\uff08string\uff09\u8f6c\u6362\u4e3a\u6574\u6570\uff08int\uff09\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e0b\u9762\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u8f6c\u6362\u65b9\u5f0f\uff1a 1. \u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-744","post","type-post","status-publish","format-standard","hentry","category-php-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":745,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/744\/revisions\/745"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}