Iterating FOREACH arrays in a FOR loop

I've been trying to learn PHP for the past week and I'm stuck at a particular problem. Please excuse my poorly worded title as I haven't grasped the jargon yet. Here's my code: $query = "SELECT $tablefields FROM $tablename"; $sth = $dbconn->prepare($query); for ($x = 1; $x <=3; $x++){ echo '<br /> This is an iteration ',$x; $sth->execute(); for

在FOR循环中迭代FOREACH数组

我一直在努力学习过去一周的PHP,并且陷入了一个特定的问题。 请原谅我题目不佳,因为我还没有掌握行话。 这是我的代码: $query = "SELECT $tablefields FROM $tablename"; $sth = $dbconn->prepare($query); for ($x = 1; $x <=3; $x++){ echo '<br /> This is an iteration ',$x; $sth->execute(); foreach ($sth->fetchall() as $row) { $results = $row[$tablefields

Concatenating strings or using multiple echo parameters: which is faster?

Suppose I have two PHP statements: echo "foo"."bar" echo "foo", "bar" Notice the different ways of concatenating the strings - using a . or a , . I realise the actual differences between the two methods, that using , gives multiple parameters to the keyword echo , whereas using . actually joins the strings together before echo ing. But my question is, which way is faster? Aotoki's

连接字符串或使用多个回显参数:哪个更快?

假设我有两个PHP语句: echo "foo"."bar" echo "foo", "bar" 注意串联字符串的不同方式 - 使用a . 或者, 。 我意识到两种方法之间的实际区别,即使用,给关键字echo多个参数,而使用. 实际上在echo之前将字符串连接在一起。 但我的问题是,哪种方式更快? Aotoki的回答是不真实的。 双引号字符串可以包含一个变量。 逗号和点与变量与文字字符串无关。 当使用echo(一种“语言结构”)时,可以声明多个参数。 这样做

Display a part of image using binary data

I wonder if it is possible to display a part of image using file_get_contents . Currently I'm using this code to display the image without revealing its link/location <img src="data:image/gif;base64,<?php echo base64_encode(file_get_contents($image)); ?> "> Is there a way to just display 1/3 or 1/2 of the image using file_get_contents ? To do this, you need to decode, crop a

使用二进制数据显示图像的一部分

我想知道是否可以使用file_get_contents显示图像的一部分。 目前,我正在使用此代码来显示图片而不透露其链接/位置 <img src="data:image/gif;base64,<?php echo base64_encode(file_get_contents($image)); ?> "> 有没有办法使用file_get_contents显示1/3或1/2的图像? 为此,您需要对图像进行解码,剪裁和重新编码。 在PHP中,您可以使用GD库来执行此操作,例如: # load source image and get its size $i

PHP string concatenation using ","?

I just discovered something. echo $var1 , " and " , $var2; is the same as: echo $var1 . " and " . $var2; What is the actual string concatenation operator in php? Should I be using . or , ? The . operator is the concatenation operator. Your first example only works because the echo 'function' (technically it's a language construct, but lets not split hairs) accepts more than

使用“,”PHP字符串连接?

我刚发现了一些东西。 echo $var1 , " and " , $var2; 是相同的: echo $var1 . " and " . $var2; 什么是PHP中的实际字符串连接运算符? 我应该使用. 或者, ? 这个. 运算符是连接运算符。 您的第一个示例仅适用于echo函数(技术上它是一种语言结构,但不允许分割头发)接受多个参数,并且会打印每个参数。 因此,第一个示例是使用多个参数调用echo,并且它们都被打印出来,而第二个示例中所有字符串都是concatenate

php concatenation operator dot or comma?

This question already has an answer here: Difference between period and comma when concatenating with echo versus return? 6 answers It's . for Concatenation. Your code should to be as, $b = "HTML"; $a = "test" . $b . "printing"; echo $a; Output : testHTMLprinting For whitespace, you should use $b = "HTML"; $a = "test" . " " . $b . " " . "printing"; echo $a; Output : test HTML p

PHP连接运算符点或逗号?

这个问题在这里已经有了答案: 与echo和return连接时的句号和逗号之间的区别? 6个答案 是的. 用于连接。 你的代码应该是, $b = "HTML"; $a = "test" . $b . "printing"; echo $a; 输出: testHTMLprinting 对于空格,你应该使用 $b = "HTML"; $a = "test" . " " . $b . " " . "printing"; echo $a; 输出: test HTML printing

Difference between period and comma when concatenating with echo versus return?

I just found that this will work: echo $value , " contiue"; but this does not: return $value , " contiue"; While "." Works in both. What is the difference between a period and a comma here? return does only allow one single expression. But echo allows a list of expressions where each expression is separated by a comma. But note that since echo is not a function but a special

与echo和return连接时的句号和逗号之间的区别?

我只是发现这将起作用: echo $value , " contiue"; 但是这不: return $value , " contiue"; 虽然“。” 适用于两者。 这里的时间和逗号之间有什么区别? return只允许一个表达式。 但是, echo允许使用逗号分隔每个表达式的表达式列表。 但请注意,由于echo不是函数,而是特殊的语言结构,因此将表达式列表包装在括号内是非法的。 你还必须注意到,作为构造的echo比用逗号更快。 所以,如果你加入一个角色400万次

date minus seconds always returns same

echo date('Y-m-d H:m:s', strtotime('15 minutes ago')) . "<br/>"; echo date('Y-m-d H:m:s', time()-900) . "<br/>"; echo date('Y-m-d H:m:s', time()); 我在Windows 7上运行WAMPP,并且所有3次调用都返回同一时间。 'H:m:s' should be 'H:i:s' ^ ^ Spot the difference m = Months i = Minutes The error in your code: 'Ymd H:m:s' should be 'Ymd H:i:s' m is m

日期减去秒总是返回相同

echo date('Y-m-d H:m:s', strtotime('15 minutes ago')) . "<br/>"; echo date('Y-m-d H:m:s', time()-900) . "<br/>"; echo date('Y-m-d H:m:s', time()); 我在Windows 7上运行WAMPP,并且所有3次调用都返回同一时间。 'H:m:s' should be 'H:i:s' ^ ^ 指出不同 m =几个月 我=分钟 你的代码中的错误: 'Ymd H:m:s'应该是'Ymd H:i:s' m是月份,我是分钟

PHP Timezone different then Centos date

I'm having some issues with my php script. For some reason the date function gives a totally different time then my servers date function. For instance php date gives me: Saturday 28th of March 2015 01:05:12 AM While centos date gives me: Fri Mar 27 18:05:12 PDT 2015 I noticed the issue because my contab's which are scheduled to run at midnight are running at the wrong time despite

PHP时区与Centos日期不同

我遇到了一些与我的PHP脚本有关的问题。 由于某些原因,日期函数给出了完全不同的时间,然后我的服务器日期函数。 例如,php的日期给了我: Saturday 28th of March 2015 01:05:12 AM 虽然centos date给了我: Fri Mar 27 18:05:12 PDT 2015 我注意到了这个问题,因为尽管服务器日期时间正确,但我的contab's计划在午夜运行,但运行时间错误。 我不明白发生了什么事情,因为直到几天前这工作得很好。 “完全不同的

PHP Convert HTML Formatted Date

Published Date returned from Twitter Search API Atom Feed as 2008-11-03T21:30:06Z which needs to be converted to "X seconds/minutes/hours/days ago" for showing how long ago twitter messages were posted. Think this can be done with php date() function using DATE_ATOM value? function time_since($your_timestamp) { $unix_timestamp = strtotime($your_timestamp); $seconds = time() -

PHP转换HTML格式化日期

从Twitter搜索API Atom Feed返回的发布日期为2008-11-03T21:30:06Z,需要将其转换为“X秒/分钟/小时/天前”,以显示多久之前发布的Twitter消息。 认为这可以通过使用DATE_ATOM值的php date()函数来完成? function time_since($your_timestamp) { $unix_timestamp = strtotime($your_timestamp); $seconds = time() - $unix_timestamp; $minutes = 0; $hours = 0; $days = 0; $weeks = 0; $mont

PHP's DateTime::Diff gets it wrong?

DateTime::Diff should calculate a proper interval and take into account Daylight Savings Time (DST) and leap years. Although apparently it isn't so. Code of horror: $d1 = new DateTime("2011-10-30 01:05:00", new DateTimeZone("Europe/Stockholm")); $d2 = new DateTime("2011-10-30 03:05:00", new DateTimeZone("Europe/Stockholm")); echo $d1->getOffset() / (60 * 60); Prints '2'! Keep

PHP的DateTime :: Diff得到它错误?

DateTime :: Diff应计算适当的时间间隔并考虑夏令时(DST)和闰年。 虽然显然不是这样。 恐怖代码: $d1 = new DateTime("2011-10-30 01:05:00", new DateTimeZone("Europe/Stockholm")); $d2 = new DateTime("2011-10-30 03:05:00", new DateTimeZone("Europe/Stockholm")); echo $d1->getOffset() / (60 * 60); 打印'2'! 请记住,UTC时间= 1小时 - 2小时= 23:05:00前一天。 echo $d2->getOffset() / (60