{"id":447,"date":"2025-06-10T20:34:50","date_gmt":"2025-06-10T12:34:50","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/447.html"},"modified":"2025-06-10T20:34:50","modified_gmt":"2025-06-10T12:34:50","slug":"php%e5%bc%ba%e5%88%b6%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e8%bf%90%e7%ae%97%e7%ac%a6%ef%bc%9a%e5%ae%9e%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/447.html","title":{"rendered":"PHP\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u8fd0\u7b97\u7b26\uff1a\u5b9e\u7528\u6307\u5357"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;&#31867;&#22411;&#36716;&#25442;&#25805;&#20316;&#31526;&#65288;casting operators&#65289;&#23545;&#20110;&#30830;&#20445;&#25968;&#25454;&#31867;&#22411;&#30340;&#21305;&#37197;&#33267;&#20851;&#37325;&#35201;&#65292;&#20174;&#32780;&#23454;&#29616;&#39044;&#26399;&#30340;&#21442;&#25968;&#31867;&#22411;&#12290;&#26412;&#25351;&#21335;&#28145;&#20837;&#25506;&#35752;&#20102;&#31867;&#22411;&#36716;&#25442;&#30340;&#32454;&#33410;&#65292;&#20174;&#22522;&#30784;&#30693;&#35782;&#21040;&#39640;&#32423;&#20351;&#29992;&#25552;&#20379;&#20102;&#28165;&#26224;&#30340;&#36335;&#24452;&#12290;<\/p>\n<h2>&#29702;&#35299;&#31867;&#22411;&#36716;&#25442;<\/h2>\n<p>&#31867;&#22411;&#36716;&#25442;&#22312;PHP&#20013;&#30340;&#36807;&#31243;&#26159;&#20174;&#19968;&#31181;&#25968;&#25454;&#31867;&#22411;&#36716;&#25442;&#20026;&#21478;&#19968;&#31181;&#25968;&#25454;&#31867;&#22411;&#12290;&#36825;&#26159;&#22240;&#20026;PHP&#26159;&#19968;&#31181;&#26494;&#25955;&#31867;&#22411;&#30340;&#35821;&#35328;&#65292;&#26377;&#26102;&#33258;&#21160;&#31867;&#22411;&#36716;&#25442;&#19981;&#36275;&#20197;&#23436;&#25104;&#24403;&#21069;&#30340;&#20219;&#21153;&#12290;&#36825;&#31181;&#36716;&#25442;&#36890;&#36807;&#22312;&#20540;&#25110;&#21464;&#37327;&#21069;&#38754;&#20351;&#29992;&#22278;&#25324;&#21495;&#26469;&#25191;&#34892;&#65306;<code> (int) $variable;<\/code>&#22909;&#30340;&#65292;&#35831;&#21457;&#36865;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p>\n<pre><code>$a = '10';   \/\/ $a is a string\n$b = (int) $a; \/\/ $b is now an integer (10)\n$c = (bool) $a; \/\/ $c is now a boolean (true)<\/code><\/pre>\n<p>&#27599;&#31181;&#31867;&#22411;&#30340;cast&#25552;&#20379;&#20102;&#19968;&#31181;&#19981;&#21516;&#30340;&#25805;&#20316;&#65292;&#29992;&#20110;&#23558;&#21407;&#22987;&#25968;&#25454;&#36716;&#25442;&#20026;&#26032;&#30340;&#24418;&#24335;&#12290;<\/p>\n<h2>&#22522;&#26412;&#30340;&#36171;&#20540;&#25805;&#20316;&#31526;<\/h2>\n<p>PHP &#25552;&#20379;&#20102;&#19981;&#21516;&#30340;&#31867;&#22411;&#36716;&#25442;&#36816;&#31639;&#31526;&#12290;&#20363;&#22914;&#65292;&#22522;&#26412;&#30340;&#31867;&#22411;&#36716;&#25442;&#36816;&#31639;&#31526;&#26377;&#65306;<code>(int)<\/code>&#22909;&#30340;&#65292;&#35831;&#21457;&#36865;&#20320;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>(float)<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>(string)<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>(bool)<\/code>&#21644;&#20320;&#19968;&#26679;&#65292;&#25105;&#20063;&#20250;&#29992;&#20013;&#25991;&#22238;&#31572;&#24744;&#30340;&#38382;&#39064;&#12290;<code>(array)<\/code>&#36825;&#20123;&#25805;&#20316;&#31526;&#29992;&#20110;&#26174;&#24335;&#22320;&#23558;&#20540;&#36716;&#25442;&#20026;&#29305;&#23450;&#31867;&#22411;&#12290;<\/p>\n<pre><code>$integer = (int) '100';   \/\/ casts '100' as an integer\n$float = (float) '3.14'; \/\/ casts '3.14' as a float\n$string = (string) 1024;  \/\/ casts 1024 as a string\n$boolean = (bool) 0;    \/\/ casts 0 as false\n$array = (array) $string; \/\/ casts string as array<\/code><\/pre>\n<h2>&#23558;&#23545;&#35937;&#36716;&#25442;&#20026;&#25968;&#32452;<\/h2>\n<p>&#36825;&#20010;&#25805;&#20316;&#22312;&#38656;&#35201;&#20462;&#25913;&#23545;&#35937;&#23646;&#24615;&#25110;&#35843;&#35797;&#26102;&#29305;&#21035;&#26377;&#29992;&#65292;&#32780;&#19981;&#24433;&#21709;&#20854;&#26041;&#27861;&#30340;&#34892;&#20026;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code>class SimpleObject {\n    public $prop1 = 'value1';\n    public $prop2 = 'value2';\n}\n\n$object = new SimpleObject();\n$array = (array) $object;<\/code><\/pre>\n<p>&#29616;&#22312;&#65292;$array &#21253;&#21547;&#20102; SimpleObject &#30340;&#23646;&#24615;&#20316;&#20026;&#20851;&#32852;&#25968;&#32452;&#12290;<\/p>\n<h2>&#21160;&#24577;&#31867;&#22411;&#36716;&#25442;&#65292;&#20351;&#29992;&#21464;&#37327;&#31867;&#22411;<\/h2>\n<p>&#20351;&#29992;&#21160;&#24577;&#31867;&#22411;&#36716;&#25442;&#65292;&#25105;&#20204;&#21487;&#20197;&#26681;&#25454;&#38656;&#35201;&#21160;&#24577;&#30830;&#23450;&#20540;&#30340;&#31867;&#22411;&#36827;&#34892;&#36716;&#25442;&#12290;<\/p>\n<pre><code>$type = 'float';\n$value = '12.34';\n$castingResult = ($type) $value; \/\/ casts '12.34' as a float<\/code><\/pre>\n<h2>&#39764;&#26415;&#20013;&#30340;&ldquo;&#21464;&#33080;&rdquo;&#26159;&#19968;&#31181;&#31867;&#22411;&#26434;&#32781;&#12290;<\/h2>\n<p>PHP &#20063;&#21487;&#20197;&#33258;&#21160;&#36716;&#25442;&#25968;&#25454;&#31867;&#22411;&#65292;&#31216;&#20026;&#31867;&#22411;&#25238;&#21160;&#12290;&#20363;&#22914;&#65292;&#22312;&#25968;&#23398;&#19978;&#19979;&#25991;&#20013;&#20351;&#29992;&#21253;&#21547;&#25968;&#23383;&#25968;&#25454;&#30340;&#23383;&#31526;&#20018;&#26102;&#65292;PHP &#23558;&#35813;&#23383;&#31526;&#20018;&#35270;&#20026;&#25968;&#20540;&#31867;&#22411;&#12290;<\/p>\n<pre><code>$sum = '1' + '1'; \/\/ PHP automatically converts strings to integers, result is 2<\/code><\/pre>\n<p>&#28982;&#32780;&#65292;&#25105;&#20204;&#24212;&#35880;&#24910;&#34892;&#20107;&#65292;&#22240;&#20026;&#36825;&#21487;&#33021;&#20250;&#23548;&#33268;&#24847;&#24819;&#19981;&#21040;&#30340;&#32467;&#26524;&#12290;<\/p>\n<h2>&#31867;&#22411;&#36716;&#25442;&#21644;&#27604;&#36739;&#36816;&#31639;&#31526;<\/h2>\n<p>&#22312;&#27604;&#36739;&#19981;&#21516;&#31867;&#22411;&#30340;&#20540;&#26102;&#65292;&#38656;&#35201;&#27880;&#24847;&#36825;&#19968;&#28857;&#65306;&#27604;&#36739;&#36816;&#31639;&#31526;&#21487;&#33021;&#20250;&#23548;&#33268;&#31867;&#22411;&#36716;&#25442;&#65288;type juggling&#65289;&#12290;<\/p>\n<pre><code>if ('100' == 100) {\n    \/\/ this evaluates to true because '100' is type-juggled to an integer\n}<\/code><\/pre>\n<p>&#20351;&#29992;&#20005;&#26684;&#30340;&#27604;&#36739;&#65288;&#21253;&#25324;&#31867;&#22411;&#26816;&#26597;&#30340;===&#25110;!==&#65289;&#21487;&#20197;&#36991;&#20813;&#36825;&#20123;&#38382;&#39064;&#12290;<\/p>\n<h2>&#39640;&#32423;&#38136;&#36896;&#25216;&#26415;<\/h2>\n<p>&#22312;&#22522;&#26412;&#31867;&#22411;&#36716;&#25442;&#20043;&#22806;&#65292;&#26356;&#22797;&#26434;&#30340;&#22330;&#26223;&#65292;&#22914;&#23558;&#19968;&#20010;&#31867;&#36716;&#25442;&#20026;&#21478;&#19968;&#20010;&#31867;&#25110;&#22312;&#36716;&#25442;&#21518;&#25805;&#20316;&#20540;&#65292;&#37117;&#36890;&#36807;&#23545;&#35937;&#23548;&#21521;&#30340;&#29305;&#24615;&#21450;&#26041;&#27861;&#26469;&#22788;&#29702;&#12290;<\/p>\n<pre><code>\/\/ casting an object of one class to another class\n$sourceObject = new SourceClass();\n$destinationObject = new DestinationClass();\n\/\/ Assume both classes have the same properties but different methods\n$destinationObject = (object) ((array) $sourceObject);<\/code><\/pre>\n<p>&#36825;&#20123;&#25216;&#26415;&#24182;&#19981;&#26159;&#26631;&#20934;&#30340;&#24211;&#65292;&#32780;&#26159;&#38656;&#35201;&#21019;&#24314;&#25968;&#32452;&#25110;&#23545;&#35937;&#26469;&#27169;&#25311;&#25152;&#38656;&#30340;&#32467;&#26500;&#12290;<\/p>\n<h2>&#22312;PHP&#20013;&#30340;&#26368;&#20339;&#23454;&#36341;&#21253;&#25324;&#65306;&#36873;&#25321;&#36866;&#24403;&#30340;&#20989;&#25968;&#65292;&#36991;&#20813;&#20351;&#29992;&#20840;&#23616;&#21464;&#37327;&#65292;&#30830;&#20445;&#20195;&#30721;&#30340;&#21487;&#35835;&#24615;&#21644;&#21487;&#32500;&#25252;&#24615;&#65292;&#20197;&#21450;&#36981;&#24490;PECL&#65288;PHP Extension Community Library&#65289;&#26631;&#20934;&#31561;&#12290;<\/h2>\n<p>&#20026;&#20102;&#36991;&#20813;&#38169;&#35823;&#24182;&#30830;&#20445;&#20195;&#30721;&#25972;&#27905;&#19988;&#26131;&#20110;&#32500;&#25252;&#65292;&#22987;&#32456;&#26126;&#30830;&#31867;&#22411;&#36716;&#25442;&#65292;&#36991;&#20813;&#19981;&#24517;&#35201;&#30340;&#31867;&#22411;&#28151;&#28102;&#65292;&#24182;&#20248;&#20808;&#20351;&#29992;&#20005;&#26684;&#30340;&#27604;&#36739;&#36816;&#31639;&#31526;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#31867;&#22411;&#36716;&#25442;&#26159;&#19968;&#31181;&#22312;PHP&#20013;&#38750;&#24120;&#24378;&#22823;&#19988;&#26377;&#25216;&#24039;&#24615;&#22320;&#20351;&#29992;&#30340;&#21151;&#33021;&#12290;&#29702;&#35299;&#20854;&#32454;&#24494;&#20043;&#22788;&#21487;&#20197;&#26174;&#33879;&#22686;&#24378;&#24744;&#30340;&#32534;&#31243;&#33021;&#21147;&#24182;&#25552;&#39640;&#24212;&#29992;&#31243;&#24207;&#30340;&#21487;&#38752;&#24615;&#12290;&#26412;&#25351;&#21335;&#20026;&#22312;&#24744;&#30340;PHP&#24320;&#21457;&#27969;&#31243;&#20013;&#26377;&#25928;&#38598;&#25104;&#25216;&#33021;&#22411;&#31867;&#22411;&#36716;&#25442;&#25552;&#20379;&#36335;&#32447;&#22270;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#22312;PHP&#20013;&#65292;&#31867;&#22411;&#36716;&#25442;&#25805;&#20316;&#31526;&#65288;casting operators&#65289;&#23545;&#20110;&#30830;&#038;#2..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-447","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=447"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=447"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}