繁体   English   中英

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

[英]how to put php code in a php if else statement

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

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

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

我下面的代码工作,因为它说真或假。 如果它是假的,我需要运行下面的代码,如果它是真的,则什么都不运行。

如果为 false 则使用此:

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

            <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"],
        ];
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM