Php IF语句忽略第二个嵌套IF语句
我编写了下面的代码和第二条嵌套的IF语句被忽略。如果我改变它们的顺序,同样的事情也会发生。我尝试了一个elseif语句,但似乎也不起作用 你知道为什么吗?我猜这是一个很难回答的问题,所以请问我:)Php IF语句忽略第二个嵌套IF语句,php,wordpress,facebook,woocommerce,Php,Wordpress,Facebook,Woocommerce,我编写了下面的代码和第二条嵌套的IF语句被忽略。如果我改变它们的顺序,同样的事情也会发生。我尝试了一个elseif语句,但似乎也不起作用 你知道为什么吗?我猜这是一个很难回答的问题,所以请问我:) !函数(f,b,e,v,n,t,s){if(f.fbq)返回;n=f.fbq=function(){n.callMethod? n、 callMethod.apply(n,参数):n.queue.push(参数)};如果(!f._fbq)f._fbq=n; n、 push=n;n.load=!0;n
!函数(f,b,e,v,n,t,s){if(f.fbq)返回;n=f.fbq=function(){n.callMethod?
n、 callMethod.apply(n,参数):n.queue.push(参数)};如果(!f._fbq)f._fbq=n;
n、 push=n;n.load=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t、 src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(窗口,
文档,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init','169969516705075');
fbq(“轨迹”、“页面视图”);
如果($product_id==8421 | |$product_id==1925 | |$product_id==1932)
如果($product|id==8421 | |$product|id==1925 | |$product|id==1932)
其他答案解决了您的核心问题,也就是说,PHP并没有像您认为的那样,对这句话:
if ($product_id == 8421 || 1925 || 1932) {...
这将始终评估为真,因为您正在说:
if $product_id == 8421 (which does a comparison as intended)
OR
1925 (which is not a comparison, but rather is a non-zero integer, which is "truthy")
OR
1932 (which is another non-zero integer, which is also "truthy")
then...
因此,作为一种编码风格,每当我检查的值超过几个时,我喜欢在数组中使用:
if ( in_array( $product_id, array( 1925, 1932, 8421 ) ) {...
不过,你正在做的事情还有很多,我也想帮你
WordPress最佳实践不会让您将代码放在模板文件的顶部,而是放在函数文件中
因此,使用相同的代码,但要像这样修改它,并将其放在主题的functions.php
文件中
请注意,它将基于wp_head hook(我在下面也包括了这个钩子)进行调用,它使您的模板文件不受逻辑/功能的影响:
// First, hook into the "wp_head" so that this is output in the head
add_action('wp_head','facebook_output');
<?php
// This function is called by the 'wp_head' action above
function facebook_output() {
// Close the PHP tag to output some script simply
?>
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '169969516705075');
fbq('track', "PageView");
<?php // Re-open the PHP tag so we can do our PHP logic
if($_GET['order_total'] && $_GET['product_id']) {
$order_total = $_GET['order_total'];
$product_id = $_GET['product_id'];
// For maintainability and readability, put the array(s) of values into variables
$products_purchase = array(8421, 1925, 1932);
$products_registration = array(1647);
// Check if the product_id is in the array
if( in_array( $product_id, $products_purchase ) ) {
// No need to assign to $output and then echo - just echo
echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
}
if( in_array( $product_id, $products_registration ) ) {
echo "fbq('track', 'CompleteRegistration');";
}
}
if ( is_cart() ) {
echo "fbq('track', 'AddToCart');";
}
if( is_checkout() ) {
echo "fbq('track', 'InitiateCheckout');";
}
if ( is_wc_endpoint_url( 'order-received' ) ) {
echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
}
// Close the PHP tag again to output the straight script
?>
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=169969516705075&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<?php // And finally re-open the PHP tag
}
//首先,钩住“wp_头”,以便在头中输出
添加_操作('wp_head','facebook_output');
!函数(f,b,e,v,n,t,s){if(f.fbq)返回;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,参数):n.queue.push(参数)};if(!f._fbq)f.\u fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(参数)[0];parents](窗口、文档、“脚本”和https://connect.facebook.net/en_US/fbevents.js');
fbq('init','169969516705075');
fbq(“轨迹”、“页面视图”);
其他答案解决了您的核心问题,即PHP并没有像您认为的那样使用以下语句:
if ($product_id == 8421 || 1925 || 1932) {...
这将始终评估为真,因为您正在说:
if $product_id == 8421 (which does a comparison as intended)
OR
1925 (which is not a comparison, but rather is a non-zero integer, which is "truthy")
OR
1932 (which is another non-zero integer, which is also "truthy")
then...
因此,作为一种编码风格,每当我检查的值超过几个时,我喜欢在数组中使用:
if ( in_array( $product_id, array( 1925, 1932, 8421 ) ) {...
不过,你正在做的事情还有很多,我也想帮你
WordPress最佳实践不会让您将代码放在模板文件的顶部,而是放在函数文件中
因此,使用相同的代码,但要像这样修改它,并将其放在主题的functions.php
文件中
请注意,它将基于wp_head hook(我在下面也包括了这个钩子)进行调用,它使您的模板文件不受逻辑/功能的影响:
// First, hook into the "wp_head" so that this is output in the head
add_action('wp_head','facebook_output');
<?php
// This function is called by the 'wp_head' action above
function facebook_output() {
// Close the PHP tag to output some script simply
?>
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '169969516705075');
fbq('track', "PageView");
<?php // Re-open the PHP tag so we can do our PHP logic
if($_GET['order_total'] && $_GET['product_id']) {
$order_total = $_GET['order_total'];
$product_id = $_GET['product_id'];
// For maintainability and readability, put the array(s) of values into variables
$products_purchase = array(8421, 1925, 1932);
$products_registration = array(1647);
// Check if the product_id is in the array
if( in_array( $product_id, $products_purchase ) ) {
// No need to assign to $output and then echo - just echo
echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
}
if( in_array( $product_id, $products_registration ) ) {
echo "fbq('track', 'CompleteRegistration');";
}
}
if ( is_cart() ) {
echo "fbq('track', 'AddToCart');";
}
if( is_checkout() ) {
echo "fbq('track', 'InitiateCheckout');";
}
if ( is_wc_endpoint_url( 'order-received' ) ) {
echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
}
// Close the PHP tag again to output the straight script
?>
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=169969516705075&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<?php // And finally re-open the PHP tag
}
//首先,钩住“wp_头”,以便在头中输出
添加_操作('wp_head','facebook_output');
!function(f,b,e,v,n,t,s){if(f.fbq)返回;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,参数):n.queue.push(参数)};if(!f.(u fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(参数)](窗口、文档、“脚本”和https://connect.facebook.net/en_US/fbevents.js');
fbq('init','169969516705075');
fbq(“轨迹”、“页面视图”);
无效的phpif($product|id==8421 | 1925 | 1932){
@nogad php是有效的,但条件是无效的。@AndrejLudinovskov true“valid”不是最好的单词选择第二个嵌套的if语句我读作if($product|id==1647){
,我不认为这就是你在这里的意思。你能更明确一点吗?无效的phpif($product|id==8421 | | 1925 | | 1932){
@nogad php有效,但条件无效。@AndrejLudinovskov true'valid'不是最佳的单词选择第二个嵌套if语句我读作if($product|id==1647){
,我不认为这就是你在这里的意思。你能更明确一点吗?这是一种重写表达式使其按预期工作的方法。这是一种重写表达式使其按预期工作的方法。它比我涵盖得更好,所以删除我的答案。@nogad-我对你的答案投了更高的票。我认为你不应该删除它。这是一个很好的“快速”解释-我的解释很冗长。这是一个很好的答案,但我支持@nogad重新添加快速解释的请求,以便两者都出现。效果完美。感谢您的深入解释!比我的解释涵盖得更好,所以删除我的答案。@nogad-我对您的答案投了更高的票。我认为您不应该删除它。这是一个很好的“快速”解释-我的解释非常冗长。这是一个很好的回答,但我支持@nogad重新添加快速解释的请求,以便两者都存在。效果完美。感谢您的深入解释!