【发布时间】:2020-01-06 01:03:14
【问题描述】:
我正在尝试根据 URL 所在的站点运行特定的 php 代码。
问题是我们的开发团队不想创建另一个页面,但是有 2 个页面使用相同的文件(我在下面粘贴的那个)。当 if 语句为 false 时,我需要一页来使用返回的数据,如果它为 true,则不需要任何其他数据来运行...
我正在尽我所能解释这一点,如果令人困惑,请原谅我。
我下面的代码的工作原理是真或假。如果它是假的,我需要运行以下内容,如果它是真的,我不需要运行。
如果为假就使用这个:
$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>
【问题讨论】:
-
这样做的目的是什么?一起写入不会显着降低性能,但将其拆分为两个文件只会增加 I/O 成本(尽管影响很小)。
-
我不想把它放在我的问题中,因为我认为它不会有帮助。我是一家公司的新开发人员,现有的系统是新的和定制的。所以在这种情况下,我们想在我们的后端添加一个功能(我已经完成并且它有效),但是我必须为此编辑的文件被两个页面使用。第二个页面(登录)并没有使用所有的功能,只需要一些小东西就可以工作。因此,我为新功能添加的新代码会引发错误。通过添加 if 语句,我可以告诉 php 为一个页面使用一组数据。
标签: php if-statement variables