如何将 php 代码放入 php if else 语句中

我正在尝试根据 URL 所在的站点运行特定的 php 代码。


问题是我们的开发团队不想创建另一个页面,但是有 2 个页面使用相同的文件(我在下面粘贴的那个)。当if语句为假时,我需要一页来使用返回的数据,如果它是真的,它不需要任何其他数据来运行......


我正在尽力解释这一点,如果它令人困惑,请原谅我。


我下面的代码的工作原理是对或错。如果它是假的,我需要运行以下内容,如果它是真的,我不需要运行。


如果为假,请使用此:


$primaryCta = [

  'text' => $data['spclprictatxt'],

  'href' => $data['spclprictalnk'],

];"

如果为真,则不使用任何内容


<?php

    use Febe\Helper\BeautifyText;

    $image = $data['image'][0] ?? null;


    $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" .

    $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];


    $pageCk = $link;


    $primaryCta = "false";


    $primaryCta1 = "true";


    if ($link === 'http://heiferdev.local/account/login.html') {

             echo $primaryCta1;

             echo $link;

    } else {

             echo $primaryCta;

             echo $link;

    }

?>


<div class="headerSimple"

    <?= !empty($data['background_color']) ? 'data-background-color="' . $data['background_color'] . '"' : ''; ?>

>

    <div class="wrappers__wrapper-max">

        <div class="headerSimple__header">


            <?php if(!empty($image)): ?>

                <figure class="headerSimple__figure">

                    <?= $this->returnView(

                        'shared/dynamicImage/dynamicImage',

                        [

                            "source" => IMGIX_URL . $image['url'],

                            "alt" => $image['alt-text'],

                            "focalPoint" => $image['focal-point'],

                            "focalPointZoom" => $image['focal-point-zoom'],

                            "parameters" => $image['parameters'],

                            "imgClass" => "headerSimple__figure-img"

                        ]

                    );?>

                </figure>

            <?php endif; ?

胡说叔叔
浏览 80回答 1
1回答

慕哥6287543

这是我为使其正常工作所做的工作。它完美无缺。希望它可以帮助其他人在路上......$link = "http://heiferdev.local/";&nbsp; &nbsp; if ( $link != (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" .&nbsp; &nbsp; $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) {&nbsp; &nbsp; &nbsp; &nbsp; $image = $data["image"][0] ?? null;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $image = $data["image"][0] ?? null;&nbsp; &nbsp; &nbsp; &nbsp; $primaryCta = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text" => $data["spclprictatxt"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "href" => $data["spclprictalnk"],&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP