我试图从以下数据获取数据:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
但如果我想用这段代码获取player_name变量:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
Run Code Online (Sandbox Code Playgroud)
我收到错误:
注意:尝试在第9行+ var_dump()的**\htdocs\index.php中获取非对象的属性返回:NULL
var_dump($pjs) 收益:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) …Run Code Online (Sandbox Code Playgroud) 我已经压制了很长一段时间的通知而没有任何问题,但我开始怀疑我是否正在做正确的事情.我似乎无法找到任何合理的理由,为什么我不应该只是压制它们,但其他人似乎认为使用error_reporting来抑制它们是一件可怕的事情,但为什么呢?
我能找到的最接近答案的是这个问题,但这仍然远远不是我正在寻找的答案.隐藏PHP生成的所有通知是否存在某种无法预料的缺点?例如,要将POST调用中的变量包含回表单中,因为存在错误,我只需使用:
<?= $_POST['variable'] ?>
Run Code Online (Sandbox Code Playgroud)
这将生成PHP通知.要解决这个问题,我可以使用这样的东西:
<?= isset($_POST['variable']) ? $_POST['variable'] : '' ?>
Run Code Online (Sandbox Code Playgroud)
但是,这真的有必要吗?我的代码实际上是否会从中获益,而不仅仅是回显变量是否存在并可能创建PHP通知?在我看来,能够忽略通知是使用PHP的好处,因为您不必担心变量是否被定义,特别是对于这样的例子,它似乎并不重要.
我还利用PHP的能力根据它的使用方式自动更改变量的类型/转换,你经常会找到如下代码片段:
for ($i = 0; $i < $limit; $i++) $results[] = $i; // Example
Run Code Online (Sandbox Code Playgroud)
这里$results以前没有定义的,而是变成一个数组,当我尝试一个新的项目添加到它作为一个阵列.我更喜欢这样做,因为如果没有结果添加到数组并且我需要存储该信息或者出于任何原因将其转换为JSON,那么将不会定义该特定变量,从而节省额外的带宽,即使它是分钟.
$data = stdClass; // For reference, in my case this would be defined before this code
$data->results = array();
$limit = 0;
for ($i = 0; $i < $limit; $i++) $data->results[] = $i;
print json_encode($data);
// {"results":[]}
Run Code Online (Sandbox Code Playgroud)
与
$data = stdClass; // For reference
$limit …Run Code Online (Sandbox Code Playgroud) 我想检查一下:
是否可以用一个if陈述来检查这个?
检查是否===会执行该操作但抛出PHP通知.我是否真的必须检查字段是否已设置然后是否为真?
我想这样做:
$matched_tags[$tag]++
作为跟踪在循环期间找到给定 $tag 的次数的一种简单方法。
这似乎是在第一次遇到任何新 $tag 时抛出 NOTICE,因为索引未定义。PHP 友好地自动激活它,将其设置为 0 并对其进行后增量,但无论如何都会抛出 NOTICE。
现在我喜欢将通知作为最佳实践进行开发,所以我不想压制它们。但对我来说,我所做的并不值得关注。
我真的必须:
if ( ! isset ( $matched_tags[$tag] ) ) $matched_tags[$tag] = 0;
$matched_tags[$tag]++;
Run Code Online (Sandbox Code Playgroud)
哦,那太痛苦了。请告诉我有一种更优雅的方式,否则我发誓我会切换到 Perl,所以请帮助我。
我已经在一台新机器上设置了我所有的PHP内容,并且我收到了很多通知警告.
我的代码在我的旧机器上正常工作而没有错误.
例如.以下行(应获取记录集值)将引发通知:$ ID = $ rs [id];
原因是id字段缺少引号,但是在不存在的值上调用$ _GET之类的事情也会引起通知.
有谁知道这是什么原因?我喜欢在我的旧机器上保持"简单"的编码方式,而不必在记录集或大量的isset()上引用任何想法?
谢谢
我尝试过不同的make flash变种[:notice]无需重新加载.
Stackoverflow给了我这个 - 你如何使用Ajax请求处理Rail的flash?,但我找不到解决方案,这对我有用.
例如,添加到我的控制器:
def create
@entry = Entry.new(params[:entry])
respond_to do |format|
if @entry.save
format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
format.js {
flash.now[:notice] = 'Website was successfully created.'
render action: 'create'
}
else
format.html { render action: "new" }
format.js { render action: "new" }
end
end
end
Run Code Online (Sandbox Code Playgroud)
create.js
$('<%= j render @website %>').appendTo('#websites').hide().fadeIn();
$(".alert").html("<%= escape_javascript(flash[:notice]) %>"); $(".alert").show(300);
$("#new_website")[0].reset();
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
有人可以告诉我可以理解的完整解决方案,这对他有用吗?
在Symfony中,当我们运行命令时,如果有任何通知或警告,Symfony会显示一条红色的大消息并退出整个程序.
我希望将错误发送到日志,但我不希望整个程序退出.任何人都可以告诉我如何实现这一目标.谢谢.
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class NotQuitWithWarningOrNoticeCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('try:nored')
->setDescription('Avoid to show big red message')
->addArgument('type');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$type = $input->getArgument('type');
if ($type == 'warning' || !$type) {
trigger_error("a warning", E_USER_WARNING);
}
if ($type == 'notice' || !$type) {
trigger_error("a notice", E_USER_NOTICE);
}
echo "Hi there!\n";
}
}
Run Code Online (Sandbox Code Playgroud)
(这里的代码只是为了重现问题.获取警告或通知是我的方案.)
默认安装的 Wordpress 是否需要欧盟 cookie 通知?我有很多人拥有非常简单的 Wordpress 网站,但我不确定即使是一个简单的 Wordpress 网站是否会在观众计算机上安装 cookie?
我一直在尝试更改空购物车消息的布局。我已经删除了操作,并尝试替换它。
我想更改 htm 结构输出:
<p class="empty-cart"></p>
Run Code Online (Sandbox Code Playgroud)
到:
<div class="col-12 offset-md-1 col-md-10"><p class="empty-cart"></p></div>
Run Code Online (Sandbox Code Playgroud)
我的实际代码(在我的主题的 functions.php 文件中):
/** Change the output for empty-cart within a div */
remove_action( 'wc_empty_cart_message', 'wc_empty_cart_message', 10 );
add_action( 'wc_empty_cart_message', 'wc_empty_cart_message', 10 );
function custom_wc_empty_cart_message() {
echo '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">'
. wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your cart is currently empty.', 'woocommerce' ) ) ) . '</p></div>';
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码不起作用。有没有人有关于如何使这项工作的建议?
在我的应用程序中,我有一个主题控制器,我需要编写一个测试用例来创建一个新主题。当一个新话题被创建时,它会被重定向到新创建话题的显示页面,并会显示一条通知“话题已成功创建!”。我需要编写一个测试用例来检查显示的通知是否正确使用 rspec。我有主题控制器:
def create
@topic = Topic.new(topic_params)
if (@topic.save)
redirect_to @topic, :notice => 'Topic was created successfully!'
else
render :action => 'new'
end
end
Run Code Online (Sandbox Code Playgroud)
主题控制器规范:
it "should create new Topic and renders show" do
expect {
post :create,params:{ topic:{topicname: "Tech"} }
}.to change(Topic,:count).by(1)
expect(response).to redirect_to(topic_path(id: 1))
/// expect().to include("Topic was created successfully!")
end
Run Code Online (Sandbox Code Playgroud)
我已经编写了重定向到显示页面的测试用例。但是我坚持检查我在代码中的评论中提到的通知。
notice ×10
php ×7
wordpress ×2
ajax ×1
cart ×1
command ×1
cookies ×1
if-statement ×1
rspec ×1
symfony ×1
syntax-error ×1
warnings ×1
woocommerce ×1