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

如何将php代码放入php if else语句中,php,if-statement,variables,Php,If Statement,Variables,我试图运行特定的php代码,这取决于URL是哪个站点 问题是我们的开发团队不想创建另一个页面,但是有两个页面使用相同的文件(我在下面粘贴的那个)。我需要一个页面来使用if语句为false时返回的数据,如果为true,则不需要任何其他数据来运行 我正在尽我所能解释这一点,请原谅我的困惑 我下面的代码的工作原理与它所说的是真是假一样。我需要运行下面的,如果它是假的,没有什么,如果它是真的 如果为false,则使用此选项: $primaryCta = [ 'text' => $data['s

我试图运行特定的php代码,这取决于URL是哪个站点

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

我正在尽我所能解释这一点,请原谅我的困惑

我下面的代码的工作原理与它所说的是真是假一样。我需要运行下面的,如果它是假的,没有什么,如果它是真的

如果为false,则使用此选项:

$primaryCta = [
  'text' => $data['spclprictatxt'],
  'href' => $data['spclprictalnk'],
];"
如果为true,则不使用任何内容

<?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; ?>

            <div class="wrappers__wrapper">
                <div class="headerSimple__headings">
                    <?php if(!empty($data['title'])) : ?>
                        <h1 class="headerSimple__title">
                            <?= BeautifyText::beautifyTitle($data['title']); ?>
                        </h1>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <?php if(!empty($data['lead_in_text']) || !empty($data['subhead'])) : ?>
            <div class="wrappers__wrapper">
                <?php if(!empty($data['subhead'])) : ?>
                    <h2 class="headerSimple__subhead">
                        <?= BeautifyText::beautifyText($data['subhead']); ?>
                    </h2>
                <?php endif; ?>
                <?php if(!empty($data['lead_in_text'])) : ?>
                     <div class="headerSimple__body">
                        <?= BeautifyText::beautifyText($data['lead_in_text'], false); ?>

                        <div class="homeHeader__cta-block">
                            <?php if (!empty($primaryCta['href']) && !empty($primaryCta['text'])) :  ?>
                                <div class="homeHeader__cta-block">
                                    <a class="button__secondary button__medium js-product-add-to-basket" href="<?= $primaryCta['href']; ?>">
                                        <?= $primaryCta['text']; ?>
                                        <img style="margin-bottom: -8px;" 
                                            src="https://toppng.com/uploads/preview/white-dollar-sign-11549435997nrl2vmgejx.png" 
                                            alt="Money Icon" 
                                            width="30px" 
                                            height="38px" >
                                    </a>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                <?php endif; ?>
            </div>
        <?php endif; ?>

    </div>
</div>

以下是我为实现这一目标所做的工作。而且它工作得完美无瑕

希望它能帮助其他人走上这条路

$link = "http://heiferdev.local/";

    if ( $link != (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" .
    $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) {
        $image = $data["image"][0] ?? null;
    } else {
        $image = $data["image"][0] ?? null;
        $primaryCta = [
            "text" => $data["spclprictatxt"],
            "href" => $data["spclprictalnk"],
        ];
    }

这样做的目的是什么?同时编写不会显著降低性能,但将其拆分为两个文件只会增加I/O成本(尽管影响很小)。但是,我不想把它放到我的问题中,因为我认为这不会有帮助。我是一家公司的新开发人员,现有的系统是新的和定制的。因此,在本例中,我们希望在后端添加一个功能(我已经完成了,并且可以正常工作),但是我为此编辑的文件被两个页面使用。第二个页面(登录)并没有使用所有的功能,只需要一些小东西就可以工作。因此,在我为新特性添加的新代码中抛出一个错误。通过添加一个if语句,我可以告诉php为一个页面使用一组数据。